ok, im following the RW tutorial on box2d, i have got to the part of adding the body to the sprites, ok heres the problem,
i can add the sprite body on the ‘cat’ sprite, but when i try to add a body to the ‘car’ sprite, it just crashes..
it works fine if i only add a body to the ‘cat’ sprite, but why cant i add a body to the ‘car’?
they both use the same method to add a body!
heres the method of adding a body:
- (void)addBoxBodyForSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,
sprite.position.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,
sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
here is the code for the ‘cat’
- (void)spawnCat {
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *cat = [CCSprite spriteWithSpriteFrameName:@"cat.jpg"];
//code here.......
[self addBoxBodyForSprite:cat];
[_spriteSheet addChild:cat];
}
heres the code for the ‘car’
- (void)spawnCar {
CCSprite *car = [CCSprite spriteWithSpriteFrameName:@"car.jpg"];
car.position = ccp(100, 100);
car.tag = 2;
[self addBoxBodyForSprite:car];
[_spriteSheet addChild:car];
}
ok with the above code it crashes, but if i remove [self addBoxBodyForSprite:car]; from the spawnCar method, then it doesnt crash, and only the ‘cat’ has a body, not the ‘car’….need help im very confused at the moment. thanks
Can you set a breakpoint at the beginning of the addBoxBodyForSprite method and step through the code to determine at what point the crash actually occurs? It is important to determine exactly what is causing the issue… everything in your method looks fine, so you would need to ensure that it is in here that the error is actually occurring.
Search for Xcode Debugging if you don’t know how to do this.