1

我有一个创建 CCSprite 并将其在屏幕上移动的函数:

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

我试图通过在移动动作中依次运行 removeFromParentAndCleanup() 来清理精灵完成移动动作。但是,每个动作都会在序列中立即触发,没有延迟。创建后立即清理精灵。为什么延迟不起作用?我已经尝试过使用和不使用 CCDelay 动作,我得到了相同的结果。

4

1 回答 1

0

解决了我自己的问题。事实证明,我对 CCActionCallBlock() 使用了错误的语法,您必须将代码块实际封装在 void 函数中,如下所示:

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

希望这可以帮助很多人,因为我看到很多人遇到这个问题,但他们从来没有得到解决方案。

于 2015-11-05T19:11:51.690 回答