Cocos2d-iPhone与Box2D:CCPhysicsSprite EXC_BAD_ACCESS [英] Cocos2d-iPhone with Box2D: CCPhysicsSprite EXC_BAD_ACCESS
问题描述
我刚刚开始混淆cocos2d的Box2D集成,虽然大多数过程是简单和直接,我使用CCPhysicsSprite(CCSprite子类,集成b2body与sprite时)一直运行到一个EXC_BAD_ACCESS错误。我使用的代码是:
I just recently started messing with cocos2d's Box2D integration, while most of the process has been simple and straight forward, I keep running into a EXC_BAD_ACCESS error when using a CCPhysicsSprite (CCSprite subclass that integrates a b2body with the sprite). The code I'm using is:
- (void)spawnBallAtPoint:(CGPoint)point
{
count++;
CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
sprite.position = point;
[self addChild:sprite];
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
b2CircleShape circleShape;
circleShape.m_radius = (26.0 / PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.8f;
body->CreateFixture(&fixtureDef);
sprite.b2Body = body;
}
这段代码触发一个EXC_BAD_ACCESS,我知道它是CCPhysicsSprite,因为改变CCPhysicsSprite为CCSprite抛出零错误:
This code triggers an EXC_BAD_ACCESS, I know it's the CCPhysicsSprite because changing CCPhysicsSprite to CCSprite throws zero errors:
- (void)spawnBallAtPoint:(CGPoint)point
{
count++;
CCSprite *sprite = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
sprite.position = point;
[self addChild:sprite];
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(point.x / PTM_RATIO, point.y / PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
b2CircleShape circleShape;
circleShape.m_radius = (26.0 / PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.8f;
body->CreateFixture(&fixtureDef);
}
我一直在环顾四周,找不到真正的答案所有的示例代码使用CCPhysicsSprite以这种方式没有错误)。我相信我犯了一个愚蠢的错误,但我想这是预期当学习新的东西:P
I've been looking around and can't find a real answer (as all the sample code uses CCPhysicsSprite in this way without error). I'm sure I'm making a stupid mistake but I guess that's to be expected when learning something new :P
感谢高级!
推荐答案
使用 CCPhysicsSprite
,你必须设置body之前设置sprite的位置。
我假设这一行是发生崩溃的地方:
With CCPhysicsSprite
you must set the body BEFORE you set the position of the sprite.
I assume that this line is where the crash occurs:
sprite.position = point;
如果你看一下 CCPhysicsSprite
position / setPosition始终使用所包含的 b2body
对象中的值。所以它真正访问它的b2body属性,这是 nil
在那一点→崩溃。
一般在创建CCPhysicsSprite之后,您希望尽快设置其.b2body - 可能会有更多的情况下,类似的问题可能发生只是因为您需要先设置.b2body属性。
If you look at CCPhysicsSprite
it overrides the position/setPosition to always use the values from the contained b2body
object. So it really accesses its b2body property, which is nil
at that point → Crash.
Generally after creating a CCPhysicsSprite you want to set its .b2body as soon as possible - there might be more occasions where similar problems might occur just because you need to set the .b2body property first.
所以,移动 sprite.position = point; 下面
sprite.b2Body = body;
。
这篇关于Cocos2d-iPhone与Box2D:CCPhysicsSprite EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!