我无法将主体添加到我的 sprite box2d 更新
好的,我按照 box2d 上的 RW 教程进行操作,我已经完成了将主体添加到精灵的部分,好吧,问题就在这里,
我可以在“猫”精灵上添加精灵主体,但是当我尝试添加主体时对于“汽车”精灵来说,它只是崩溃了.. 如果我只向“猫”精灵添加一个主体,它就可以正常工作,但为什么我不能向“汽车”添加一个主体? 他们都使用相同的方法来添加身体!
这是添加主体的方法:
- (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);
}
这是“猫”的代码
- (void)spawnCat {
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *cat = [CCSprite spriteWithSpriteFrameName:@"cat.jpg"];
//code here.......
[self addBoxBodyForSprite:cat];
[_spriteSheet addChild:cat];
}
这是“汽车”的代码
- (void)spawnCar {
CCSprite *car = [CCSprite spriteWithSpriteFrameName:@"car.jpg"];
car.position = ccp(100, 100);
car.tag = 2;
[self addBoxBodyForSprite:car];
[_spriteSheet addChild:car];
}
上面的代码可以,它会崩溃,但是如果我删除 [self addBoxBodyForSprite:car];
从spawnCar方法,那么它不会崩溃,只有“猫”有身体,没有“汽车”....需要帮助,我现在很困惑。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否在 addBoxBodyForSprite 方法的开头设置一个断点,并单步执行代码以确定崩溃实际发生的时间点?准确确定导致问题的原因非常重要......您的方法中的所有内容看起来都很好,因此您需要确保错误确实发生在此处。
如果您不知道如何执行此操作,请搜索 Xcode 调试。
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.