为操作暂停for循环 - Swift:Sprite Kit

时间:2017-07-15 12:58:10

标签: for-loop sprite-kit skaction

我正试图让一只鹿在不同的位置上跳跃。

func deerJumping () {
   // The different locations are arranged ramdonly
   var places: [CGFloat] = [1124, 852, 1540, 1908, 628, 1736, 392].shuffled()
   //Then the for loop with the actions (jumps) between this positions
   for posc in places {
        //SKAction for the up and down
        //SKAction for the displacement left or right
        //Code for the deer to face the direction of the jump
        //Then run the action:
        deer.run(SKAction.group([jump, move]))
   }
}

问题是循环没有等到动作完成才转到数组中的下一个位置。

我发现这个问题Pause and Resume a for Loop。它即将在循环中创建NSOperation个实例并将它们添加到NSOperationQueue但遗憾的是我是初学者而且我不理解解决方案所以我不知道如何将它应用于我的代码

任何帮助都将非常感谢!!

1 个答案:

答案 0 :(得分:1)

没有等待的原因是因为您同时运行所有操作。您想要创建一系列操作并按顺序运行

func deerJumping () {
   // The different locations are arranged ramdonly
   var places: [CGFloat] = [1124, 852, 1540, 1908, 628, 1736, 392].shuffled()
   //Then the for loop with the actions (jumps) between this positions
   var actions = [SKAction]()
   for posc in places {
        //SKAction for the up and down
        //SKAction for the displacement left or right
        //Code for the deer to face the direction of the jump
        //Then run the action:
        actions.append(SKAction.sequence([jump, move]))
   }
   var deerRunning = SKAction.sequence(action)
   deer.run(deerRunning)

}

现在我不知道你为什么要暂停和恢复for循环,在我能帮助你之前你必须详细说明那个。