我正试图在SpriteKit游戏中产生一波敌人。
我认为这段代码会起作用。在调用下一个wave之前,如何让runAllWaves()
等待每个wave完成? (我意识到如果所有其他方法都失败了我可以人为地增加waveWait时间从头开始计数而不是每个波的结束)
import SpriteKit
let waveWait = [5, 10, 10] // pause before each wave
let waveCount = [10, 20, 50] // num. of enemies in each wave
let waveDelay = [3, 2, 1] // pause before each enemy spawns
let waveHealth = [1, 2, 3] // enemy health for each wave
var index:Int = 0
var spawn = SKAction()
var delay = SKAction()
var spawndelay = SKAction()
var repeatspawndelay = SKAction()
var wait = SKAction()
var dowave = SKAction()
var doAllWaves = SKAction()
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
print("begin!")
runAllWaves()
}
func runWave(index:Int){
spawn = SKAction.runBlock({ self.spawnEnemy(index) })
delay = SKAction.waitForDuration(NSTimeInterval(waveDelay[index]))
spawndelay = SKAction.sequence([spawn, delay])
repeatspawndelay = SKAction.repeatAction( spawndelay , count: waveCount[index])
self.runAction(repeatspawndelay)
}
func runAllWaves(){
runAction(SKAction.sequence([
SKAction.waitForDuration(NSTimeInterval(waveWait[index])),
SKAction.runBlock({ self.runWave(index) }) ]),
completion: {
index += 1
if index < 3 { self.runAllWaves() }})
}
func spawnEnemy(index:Int){ print("spawn enemy with health: " + String(waveHealth[index])) }
}
答案 0 :(得分:0)
为什么不实施runNextWave
功能并在runWave
结束时调用它?你有定义每个wave属性的数组;如果数组中的第一个元素确定了wave的属性,那么在runWave
期间您可以删除这些条目,并在runWave
结尾处再次调用runWave
,直到数组为空或其他条件为止满足了。