Cocos2d CCSprite位置释放错误

时间:2012-03-01 20:06:07

标签: ios memory-management cocos2d-iphone sprite

当我使用以下代码运行我的游戏时,我的游戏崩溃,当玩家与敌人2相交时,我在调试控制中得到以下错误。

错误: *** -[CCSprite position]: message sent to deallocated instance 0xa8cabd0

代码:

// Check if enemy2 is a child of the layer
if (enemy2) {
    CGRect enemy2Rect = CGRectMake(
                                   enemy2.position.x - (enemy2.contentSize.width/2), 
                                   enemy2.position.y - (enemy2.contentSize.height/2), 
                                   80,   
//                   ERROR HAPPENS HERE   Stopped at thread 1       
                                   41);
    // check if player intersects the enemy
    if (CGRectIntersectsRect(playerRect, enemy2Rect)) {
        // check if the power up is true
        if (bustEmUp == TRUE) {
            enemy2Hit = TRUE;
            [self unschedule:@selector(collisionDetection)];
            [self removeChild:enemy2 cleanup:YES];
            id delay = [CCDelayTime actionWithDuration:15];
            id addEnemy = [CCCallFunc actionWithTarget:self selector:@selector(addEnemy2)];
            [self runAction:[CCSequence actions:delay,addEnemy, nil]];
            [self schedule:@selector(collisionDetection) interval:0.01];
        } else {
            // if not then collide
            [self schedule:@selector(collisionAlert)];
        }
    }
}

如果有帮助的话,继承我的回溯:

#0  0x01a83e1e in ___forwarding___ ()
#1  0x01a83ce2 in __forwarding_prep_0___ ()
#2  0x000e4fab in -[Survival collisionDetection] (self=0x12b975c0, _cmd=0x14a5e0) at Survival.m:521
#3  0x0005babb in -[CCTimer update:] (self=0x1281af50, _cmd=0x13347c, dt=0.0494979993) at CCScheduler.m:141
#4  0x00064a20 in -[CCScheduler tick:] (self=0xa8c9b70, _cmd=0x13c8fa, dt=0.0494979993) at CCScheduler.m:606
#5  0x0008d9ef in -[CCDirectorIOS drawScene] (self=0x9466d20, _cmd=0x136bdf) at CCDirectorIOS.m:152
#6  0x0008ffda in -[CCDirectorDisplayLink mainLoop:] (self=0x9466d20, _cmd=0x142bcd, sender=0x1208c2b0) at CCDirectorIOS.m:721
#7  0x005e22db in CA::Display::DisplayLink::dispatch ()
#8  0x005e21af in CA::Display::TimerDisplayLink::callback ()
#9  0x01af1966 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#10 0x01af1407 in __CFRunLoopDoTimer ()
#11 0x01a547c0 in __CFRunLoopRun ()
#12 0x01a53db4 in CFRunLoopRunSpecific ()
#13 0x01a53ccb in CFRunLoopRunInMode ()
#14 0x021c8879 in GSEventRunModal ()
#15 0x021c893e in GSEventRun ()
#16 0x00b63a9b in UIApplicationMain ()
#17 0x000c083f in main (argc=1, argv=0xbffff5a0) at main.m:14

继承人我在哪里初始化敌人2:

enemy2 = [CCSprite spriteWithFile:@"SpaceShip2.png"];
enemy2.position = ccp(500,700);
[self addChild:enemy2];

1 个答案:

答案 0 :(得分:2)

enemy2可能已被释放。它将通过你的if(enemy2)检查,因为enemy2是一个地址而不是nil,但是该地址的对象已被解除分配。