gpt4 book ai didi

swift - SpriteKit Swift 2 Action : spawning enemy waves

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:57 24 4
gpt4 key购买 nike

我正在尝试在 SpriteKit 游戏中生成一波又一波的敌人。我认为这段代码会起作用。如何让 runAllWaves() 在调用下一波之前等待每一波完成? (我意识到,如果一切都失败了,我可以人为地增加 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])) }

}

最佳答案

为什么不实现 runNextWave 功能并在 runWave 结束时调用它?您有定义每个波浪属性的数组;如果数组中的第一个元素确定了 wave 的属性,那么在 runWave 期间,您可以删除这些条目,并在 runWave 结束时再次调用 runWave直到数组为空或满足其他条件。

关于swift - SpriteKit Swift 2 Action : spawning enemy waves,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com