2016-10-11 27 views
0

我目前有一些完成塊,應該在完成滑動手勢後執行。我在應該在完成塊中調用的函數內部放置斷點,但它們從不被觸發。滑動手勢起作用,我不知道爲什麼完成塊沒有被調用。這裏是我的代碼:SpriteKit中的完成塊從未調用

import SpriteKit 


let plankName = "woodPlank" 

class PlankScene: SKScene { 

    var plankWood : SKSpriteNode? 

    var plankArray : [SKSpriteNode] = [] 





    override func didMove(to view: SKView) { 

    enumerateChildNodes(withName: plankName) { 
     node, stop in 
     self.plankWood = node as? SKSpriteNode 

     let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight)) 

     swipeRight.direction = .right 

     view.addGestureRecognizer(swipeRight) 


    } 

    } 



    func swipedRight(sender: UISwipeGestureRecognizer) { 

    if sender.direction == .right { 

     //The functions in this completion block are never called 
     swipeAndAddPlank(completion: { 
     self.addPlank(completion: { 
     self.movePlanksUp() 
     }) 

     }) 
    } 
    } 


    func swipeAndAddPlank(completion: (()->Void)?) { 

    let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5) 

    let nodeFinishedMoving = SKAction.removeFromParent() 

    plankWood?.run(SKAction.sequence([moveOffScreenRight,nodeFinishedMoving])) 


    } 


    //This function never called 
    func addPlank(completion: (()->Void)?) { 
    let newPlank = plankWood?.copy() as! SKSpriteNode 
    newPlank.position = CGPoint(x: 0, y: -259) 
    plankArray.append(newPlank) 
    print(plankArray.count) 
    addChild(newPlank) 


    } 

    //This function never called 
    func movePlanksUp() { 
    for node:SKSpriteNode in plankArray { 
     node.run(SKAction.move(by: CGVector(dx: 0, dy: 50), duration: 0.10)) 
    } 
    } 


} 

回答

0

swipeAndAddPlank不包含對completion塊傳遞給它的調用。 addPlank也一樣。您需要插入呼叫completion()。您可能正在尋找SKAction.runBlock。像

let completionAction = SKAction.runBlock({ completion() }) 

東西然後加入completionAction到plankWood設置爲運行操作的順序。

+0

這就是我要找的!但由於一些奇怪的原因,我得到一個錯誤「無法將類型'()'的值轉換爲期望的參數類型'() - > Void'」 – SwiftyJD

+0

這是我用的:let completionAction = SKAction.runBlock(addPlank {completion!( )}) – SwiftyJD

相關問題