我有一个创建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操作,但我得到的结果相同。
答案 0 :(得分: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]));
}
希望这对很多人有所帮助,因为我看到很多这个问题并且他们从来没有提供过解决方案。