我有以下代碼:的cocos2d Box2D的精靈位置崩潰
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL)
{
CCSprite *itemSprite = (CCSprite*)b->GetUserData();
CGSize SpriteSize;
SpriteSize.height = itemSprite.contentSize.height;
SpriteSize.width = itemSprite.contentSize.width;
CGPoint SpritePosition=[itemSprite position];
}
}
剛剛獲得精靈的位置,但這個方案中有時會崩潰:
SpriteSize.height = itemSprite.contentSize.height;
這行:
CGPoint SpritePosition=[itemSprite position];
我正在使用cocos2d .99.5。
Edit:
這裏是我的崩潰日誌:
異常類型:EXC_BAD_ACCESS(SIGBUS) 異常代碼:KERN_PROTECTION_FAILURE在0x0000000f 墜毀主題:0
Thread 0 Crashed:
0 libobjc.A.dylib 0x00002666 objc_msgSend_stret + 14
1 ShootTheMonkey 0x0004c066 -[LevelFour ccTouchBegan:withEvent:] (LevelFour.mm:1432)
2 ShootTheMonkey 0x00321bf0 -[CCTouchDispatcher touches:withEvent:withTouchType:] (CCTouchDispatcher.m:238)
3 ShootTheMonkey 0x00322454 -[CCTouchDispatcher touchesBegan:withEvent:] (CCTouchDispatcher.m:305)
4 ShootTheMonkey 0x003240ec -[EAGLView touchesBegan:withEvent:] (EAGLView.m:318)
5 UIKit 0x00053d72 -[UIWindow _sendTouchesForEvent:] + 254
6 UIKit 0x000536fe -[UIWindow sendEvent:] + 74
7 UIKit 0x0004f320 -[UIApplication sendEvent:] + 260
8 UIKit 0x0004ec8c _UIApplicationHandleEvent + 4772
9 GraphicsServices 0x00003b2c PurpleEventCallback + 660
10 CoreFoundation 0x00022d96 CFRunLoopRunSpecific + 2214
11 CoreFoundation 0x000224da CFRunLoopRunInMode + 42
12 GraphicsServices 0x000030d4 GSEventRunModal + 108
13 GraphicsServices 0x00003180 GSEventRun + 56
14 UIKit 0x0000342a -[UIApplication _run] + 374
15 UIKit 0x00001954 UIApplicationMain + 636
16 ShootTheMonkey 0x0000377e main (main.m:13)
17 ShootTheMonkey 0x0000373c start + 32
崩潰轉儲應該給你一些有關錯誤的確切原因的有價值的信息。你可以發佈嗎? – Ben 2011-04-20 07:20:13
我想,那個b2Body裏面沒有任何精靈。這是一個很好的習慣,將迭代通過body列表更改爲如下所示:for(b2Body * body = world-> GetBodyList(); body!=無; body => Body-> GetNext()) \t { \t \t BodyNode * bodyNode =(BodyNode *)body-> GetUserData(); \t \t如果(bodyNode = NULL && bodyNode.sprite =零!) \t \t { ....} – gixdev 2011-04-20 09:06:36
@gixdev:UR答案可能是有幫助的..... u能透露具體細節在這個答案節問題,請..... – Rony 2011-04-20 10:48:18