gpt4 book ai didi

ios - 如何在时间延迟后生成对象 swift 3 spritekit

转载 作者:搜寻专家 更新时间:2023-11-01 07:11:49 25 4
gpt4 key购买 nike

我正在开发一款射击游戏。我想每 10 秒生成一个屏障,然后在 3 秒后删除它。我认为它类似于

                let TimeBeforeAdd = DispatchTime.now() + 5

DispatchQueue.main.asyncAfter(deadline: TimeBeforeAdd) {

self.Barrier.position = self.barrierPos

self.addChild(self.Barrier)

self.barrierAdded = true





}

if barrierAdded == true {

let RemoveTime = DispatchTime.now() + 3

DispatchQueue.main.asyncAfter(deadline: RemoveTime) {

self.Barrier.removeFromParent()

self.barrierAdded = false

}


}

但是当它在十秒后运行时,我收到一条错误消息,说它添加了多个“Barrier”实例,感谢您的帮助。

最佳答案

调度计时器是个很酷的主意。但是,有一种简单得多的方法!给你:

// Inside of your gamescene:
func spawnThingEveryTenSecondsThenDeleteAfterThree() {

func spawnShootyThing() { /* input your code here */ }
func despawnShootyThing() { /* input your code here */ }

let wait10 = SKAction.wait(forDuration: 10)
let wait3 = SKAction.wait(forDuration: 3)
let spawn = SKAction.run { spawnShootyThing() }
let despawn = SKAction.run { despawnShootyThing() }

let action = SKAction.sequence([wait10, spawn, wait3, despawn])

// If you don't want this action to run forever, then remove this action!
let forever = SKAction.repeatForever(action)

self.run(forever)
}

关于ios - 如何在时间延迟后生成对象 swift 3 spritekit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44479880/

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