Reputation: 23
I am trying to get the exact position of a sprite at the time a mousejoint moving the sprite is released (even though the sprite might still be moving) and display it. I am using Cocos2d and Box2d. Below is the code ccTouchesEnded method.
ccTouchesEnded:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint)
{
b2Fixture *fixture;
CCSprite *mySprite = (CCSprite *) fixture->GetUserData();
NSInteger attachedSprite = mySprite.tag;
if (attachedSprite == 1) {
CGPoint spritePosition = mySprite.position;
CCLOG(@"the sprite position is x:%0.2f, y:%0.2f", spritePosition.x, spritePosition.y);
}
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
I keep getting the EXC_BAD_ACCESS error pointing to the line:
CCSprite *mySprite = (CCSprite *) fixture->GetUserData();
I am not really sure what's wrong. Please help.
Upvotes: 0
Views: 339
Reputation: 577
You are getting that error because fixture has not been initialized, check out this tutorial, how he loop over all the world elements
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *curSprite = (CCSprite *)b->GetUserData();
...
}
}
you have to assign something to the fixture before do the GetUserData()
Upvotes: 1