user3303990
user3303990

Reputation: 29

cocos2d v3 exception thrown removing sprite from scene

so im trying to make a tower defense game, (Towers shoot bullets at advancing enemies), so the problem is when i try to remove the bullet from the scene after it hit the enemy, it throws an exception, with no error in the debugger.

Here is the code:

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

if anyone has an idea why this is going on, any help would be greatly appreciated.

cheers

Upvotes: 0

Views: 350

Answers (1)

lucianomarisi
lucianomarisi

Reputation: 1552

The bullet is not being passed, hence the exception on removeBullet: method.

This line is the problem:

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

Add breakpoint to [bullet.parent removeChild:bullet cleanup:YES]; and po bullet on the debugger and you will probably get nil.

My solution would be to use a block action, for example:

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

Upvotes: 1

Related Questions