2017-07-15 30 views
0

我試圖讓一隻鹿通過不同的定義位置跳轉ramdonly。暫停循環以執行操作 - Swift:Sprite Kit

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

沒有什麼可以等待的原因是因爲您同時運行所有的操作。你要創建的動作的陣列和序列運行

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循環,你將有詳細說明在這之前,我可以幫你出去。

+0

非常感謝KnightOfDragon。我認爲它可以工作! :) – Sersiego

+0

我想暫停循環以便在行駛到下一個位置之前花時間運行行動。 – Sersiego

+0

我沒跟着你。暫停for循環什麼也不做 – Knight0fDragon