1

I have a function which creates a CCSprite and moves it across the screen:

func fireWeapon(target: CGPoint) {
    let projectile = CCBReader.load("Projectile") as! CCSprite
    projectile.position = player.position;
    self.addChild(projectile);

    let moveAction = CCActionMoveTo(duration: 1, position: target);
    let delayAction = CCActionDelay(duration: 1);
    let removeAction = CCActionCallBlock(projectile.removeFromParentAndCleanup(true));

    projectile.runAction(CCActionSequence(array: [moveAction, delayAction, removeAction]));
}

I'm trying to clean up the sprites after they finish their movement action by running removeFromParentAndCleanup() in sequence with the move action. However, each action is firing instantly after each other in the sequence with no delays. The sprites are cleaned up instantly after being created. Why aren't the delays working? I've tried with and without the CCDelay action and I get the same result.

Andrew
  • 11
  • 4

1 Answers1

0

Solved my own problem. Turns out that I was using the wrong syntax for CCActionCallBlock(), you have to actually encase your block of code within a void function, like so:

func fireWeapon(target: CGPoint) {
    let projectile = CCBReader.load("Projectile") as CCNode
    projectile.position = player.position;
    self.addChild(projectile);

    let moveAction = CCActionMoveTo(duration: 1, position: target);
    let delayAction = CCActionDelay(duration: 3);
    let removeAction = CCActionCallBlock { () -> Void in
        projectile.removeFromParentAndCleanup(true);
    }
    projectile.runAction(CCActionSequence(array: [moveAction, delayAction, removeAction]));
}

Hopefully this helps out a lot of people, because I saw lots of with this problem and they were never presented with a solution.

Andrew
  • 11
  • 4