Hello everybody,
I’m using Box2D in cocos2d-x 2.1.5. I follow this tutorial at http://www.raywenderlich.com/33750/cocos2d-x-tutorial-for-ios-and-android-getting-started and try to port it into cocos2d-x project.
But the game gets error at the line ballData->setPositionX(0) in the code below
void HelloWorld::tick(float dt) { _world->Step(dt, 10, 10); for (b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) { if (b->GetUserData() != NULL) { CCSprite *ballData = (CCSprite *)b->GetUserData(); ballData->setPositionX(0); } } }
Please help me!!Tks
P/S: Sorry for my bad English!!
Hi,
What kind of error did you get?
You should check that:
- All user data are set with a CCSprite instance (if not your cast is incorrect).
- All CCSprite objects have been added to a parent layer/scene (if not they’re auto released and the pointer is invalid).
BTW you don’t want to update the sprite if the body is static. It’s useless.
if (b->GetType() != b2_staticBody && b->GetUserData() != NULL)
Hope it helps, Laurent
Hi there, thanks for your response.
Finally I solved this. The problem is when I set the UserData for body, I used the ref of the CCSprite instance. lol!!!
Thanks very much!!
You’re welcome. Glad you solved it.