2012-08-08 46 views
0

我試圖在釋放鼠標連接移動精靈的時候獲取精靈的精確位置(即使精靈可能仍在移動)並顯示它。我正在使用Cocos2d和Box2d。以下是代碼ccTouchesEnded方法。EXC_BAD_ACCESS在釋放鼠標連接時獲取精靈位置的錯誤

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; 
} 
} 

我不斷收到EXC_BAD_ACCESS錯誤指向行:

CCSprite *mySprite = (CCSprite *) fixture->GetUserData(); 

我真的不知道什麼是錯的。請幫忙。

回答

1

你得到的是錯誤,因爲燈具沒有被初始化,看看這個 tutorial,他怎麼循環了世界上所有的元素

for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) { 
    if (b->GetUserData() != NULL) { 
     CCSprite *curSprite = (CCSprite *)b->GetUserData(); 
... 
    } 
} 

你要的東西分配給燈具做GetUserData之前( )

+0

謝謝。我做了更改,但我沒有出現CCLOG。下面是新ccTouchesEnded代碼: – newbie 2012-08-08 23:20:45

+0

' - (無效)ccTouchesEnded:(NSSet中*)觸摸withEvent:方法(的UIEvent *)事件 { 如果(mouseJoint) { 爲(b2Body * B =世界 - > GetBodyList(); B ; b = b-> GetNext()){if(b-> GetUserData()!= NULL)CCSprite * mySprite =(CCSprite *)b-> GetUserData(); (mySprite.tag == 1)CGPoint spritePosition = mySprite.position;如果(mySprite.tag == 1){ } CCLOG(@「精靈位置是x:%0.2f,y:%0.2f」,spritePosition.x,spritePosition.y); } } } world-> DestroyJoint(mouseJoint); mouseJoint = NULL; } }' – newbie 2012-08-08 23:26:02

+0

抱歉格式。 – newbie 2012-08-08 23:27:04