gpt4 book ai didi

swift - 如何让 SKAction.sequence 等待随机持续时间

转载 作者:行者123 更新时间:2023-11-30 10:18:40 26 4
gpt4 key购买 nike

我正在尝试打开一扇门,等待一段随机时间(在一定范围内),然后关闭。如果我使用 SKAction.waitForDuration,我可以设置确切的等待时间,这很有效。但是,如果我使用 SKAction.waitForDuration (withRange),它总是在范围内的最短时间准确打开。如何让它在范围内的其他时间打开?任何帮助将不胜感激!谢谢!

这是我的代码:

var doorAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height + size.height * 1.95), duration: NSTimeInterval(1))

// randomWait should give me a value between 10 & 30, but door always opens at 10

var randomWait = SKAction.waitForDuration(20.0, withRange: 20.0)

// waitAction works fine, but I want a value between 10 and 30
// var waitAction = SKAction.waitForDuration(10)

var doorReturnAction = SKAction.moveTo(CGPoint(x: size.width * 0.5, y: size.height * 2.18), duration: NSTimeInterval(1))

var actionSequence = SKAction.sequence([doorAction, randomWait, doorReturnAction])

self.runAction(actionSequence)

最佳答案

由于您没有重复操作序列,因此您实际上并不需要SKAction.waitForDuration:withRange:。您可以使用 arc4random() 计算 waitDuration

let upperlimit : UInt32 = 30
let lowerlimit : UInt32 = 10

let waitDuration = NSTimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)

var randomWait = SKAction.waitForDuration(waitDuration)

关于swift - 如何让 SKAction.sequence 等待随机持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598162/

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