2016-10-11 30 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

相关问题