gpt4 book ai didi

swift - 节点的新位置

转载 作者:行者123 更新时间:2023-11-30 14:07:46 25 4
gpt4 key购买 nike

我正在尝试取消隐藏此节点,以便它出现在rect位置内,但随后以某种方式再次隐藏该节点,以便当它取消隐藏时,它位于矩形内的不同位置。但我试图添加一个随机计时器,以随机间隔(但小于 5 秒)取消隐藏

let x12 = rect4.origin.x + CGFloat(arc4random()) % rect4.size.width
let y12 = rect4.origin.y + CGFloat(arc4random()) % rect4.size.height
let randomPoint12 = CGPointMake(x12, y12)
self.yellowcircle3.position = randomPoint12
self.addChild(yellowcircle3)


yellowcircle3.hidden = true

最佳答案

听起来您想制作一个 NSTimer 并使用 arc4random 来获取时间间隔。

您需要做的第一件事是通过添加代码声明一个 NSTimer 和一个随机数:

var myTimer = NSTimer()
var secondsPassed : Int = 0
var randomTimeInterval = arc4Random % 4 + 1

(4 + 1 将返回 1 到 5 之间的值)

接下来,您需要一个在想要移动节点时调用的函数。

 func moveNode(){
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerRan:"), userInfo: nil, repeats: true)

}

一旦你调用moveNode(),你的计时器就会开始运行。但现在我们需要计时器的功能,或者它是Selector,这样它才能正常运行。

func timerRan(sender: NSTimer!){
secondsPassed++
if secondsPassed == randomTimeInterval (
//unhide your rect and reset the timer
)
}

希望这有帮助:)

关于swift - 节点的新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144685/

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