Cocos2d v3异常导致精灵从场景中移除



所以我试图制作一款塔防游戏,(塔向前进的敌人发射子弹),所以问题是当我试图在击中敌人后从场景中移除子弹时,它会抛出一个异常,在调试器中没有错误。

代码如下:

-(void)shootWeapon
{
CCSprite * bullet = [CCSprite spriteWithImageNamed:@"snowball.png"];
[theGame addChild:bullet];
[bullet setPosition:mySprite.position];//Todo offset snowball to the right of the tower
[bullet runAction:[CCActionSequence actions:[CCActionMoveTo actionWithDuration:0.3     
position:chosenEnemy.mySprite.position],[CCActionCallFunc actionWithTarget:self   
selector:@selector(damageEnemy)],[CCActionCallFunc actionWithTarget:self   
selector:@selector(removeBullet:)], nil]];
}
-(void)removeBullet:(CCSprite *)bullet
{
[bullet.parent removeChild:bullet cleanup:YES];
}
-(void)damageEnemy
{
[chosenEnemy getDamaged:damage];
}

如果有人知道为什么会这样,任何帮助将是非常感激的。

欢呼

子弹没有被传递,因此在removeBullet:方法上出现了异常。

这一行就是问题所在:

[CCActionCallFunc actionWithTarget:self
                          selector:@selector(removeBullet:)]

在调试器上为[bullet.parent removeChild:bullet cleanup:YES];po bullet添加断点,您可能会得到nil。

我的解决方案是使用块操作,例如:

CCAction *blockAction = [CCActionCallBlock actionWithBlock:^{
        [bullet removeFromParentAndCleanup:YES];
    }];

最新更新