2015-11-05 24 views
1

我有它創建了一個CCSprite然後在屏幕上移動它的功能:CCActionSequence/CCActionDelay不延遲每個動作?

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

解決了我自己的問題。事實證明,我是使用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])); 
} 

希望這幫助了很多人,因爲我看到很多這個問題,他們從來沒有提出解決方案。