swift spritekit 随着时间的推移增加节点创建的频率
swift spritekit increase frequency of node creation as time goes on
我已经弄清楚如何每 x 秒连续生成一个节点。但是,我想随着游戏的进行减少等待创建节点的时间,以增加难度。比如我在didMoveToView:
中调用这个函数
func createSequentialEnemies(){
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(createEnemy),
SKAction.waitForDuration(2.0)
])
))
}
这每 2 秒创建一个敌人,但我想任意减少此持续时间。例如,假设在玩游戏 30 秒后,我现在想每 1.5 秒生成一次敌人。我将如何动态更改持续时间?
在您的场景中创建一个 spawnDuration 属性 和关键引用 class。
class SomeClass: SKScene {
private var spawnDuration: NSTimeInterval = 2.0
private let spawnKey = "SpawnKey"
}
然后调整您的生成代码以使用此生成 属性 和密钥。在我看来,我也稍微更改了语法以使其更易读。
func createSequentialEnemies(){
removeActionForKey(spawnKey) // remove previous action if running. This way you can adjust the spawn duration property and call this method again and it will cancel previous action.
let spawnAction = SKAction.runBlock(createEnemy)
let spawnDelay = SKAction.waitForDuration(spawnDuration)
let spawnSequence = SKAction.sequence([spawnAction, spawnDelay])
runAction(SKAction.repeatActionForever(spawnSequence), withKey: spawnKey) // run action with key so you can cancel it later
}
然后你必须添加一些逻辑来改变你创建的生成持续时间属性。
Time based 可以是这样的函数,您也可以在 DidMoveToView 中调用一次
func startDifficultyTimer() {
let difficultyTimerKey = "DifficultyTimerKey"
let action1 = SKAction.waitForDuration(30)
let action2 = SKAction.runBlock { [unowned self] in
guard self.spawnDuration > 0.2 else { // set a min limit
removeActionForKey(difficultyTimerKey) // if min duration has been reached than you might as well stop running this timer.
return
}
self.spawnDuration -= 0.5 // reduce by half a second
self.createSequentialEnemies() // spawn enemies again
}
let sequence = SKAction.sequence([action1, action2])
runAction(SKAction.repeatActionForever(sequence), withKey: difficultyTimerKey)
}
希望对您有所帮助
我已经弄清楚如何每 x 秒连续生成一个节点。但是,我想随着游戏的进行减少等待创建节点的时间,以增加难度。比如我在didMoveToView:
中调用这个函数func createSequentialEnemies(){
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(createEnemy),
SKAction.waitForDuration(2.0)
])
))
}
这每 2 秒创建一个敌人,但我想任意减少此持续时间。例如,假设在玩游戏 30 秒后,我现在想每 1.5 秒生成一次敌人。我将如何动态更改持续时间?
在您的场景中创建一个 spawnDuration 属性 和关键引用 class。
class SomeClass: SKScene {
private var spawnDuration: NSTimeInterval = 2.0
private let spawnKey = "SpawnKey"
}
然后调整您的生成代码以使用此生成 属性 和密钥。在我看来,我也稍微更改了语法以使其更易读。
func createSequentialEnemies(){
removeActionForKey(spawnKey) // remove previous action if running. This way you can adjust the spawn duration property and call this method again and it will cancel previous action.
let spawnAction = SKAction.runBlock(createEnemy)
let spawnDelay = SKAction.waitForDuration(spawnDuration)
let spawnSequence = SKAction.sequence([spawnAction, spawnDelay])
runAction(SKAction.repeatActionForever(spawnSequence), withKey: spawnKey) // run action with key so you can cancel it later
}
然后你必须添加一些逻辑来改变你创建的生成持续时间属性。
Time based 可以是这样的函数,您也可以在 DidMoveToView 中调用一次
func startDifficultyTimer() {
let difficultyTimerKey = "DifficultyTimerKey"
let action1 = SKAction.waitForDuration(30)
let action2 = SKAction.runBlock { [unowned self] in
guard self.spawnDuration > 0.2 else { // set a min limit
removeActionForKey(difficultyTimerKey) // if min duration has been reached than you might as well stop running this timer.
return
}
self.spawnDuration -= 0.5 // reduce by half a second
self.createSequentialEnemies() // spawn enemies again
}
let sequence = SKAction.sequence([action1, action2])
runAction(SKAction.repeatActionForever(sequence), withKey: difficultyTimerKey)
}
希望对您有所帮助