运行时修改重复的任务序列

编程入门 行业动态 更新时间:2024-10-11 11:24:05
本文介绍了运行时修改重复的任务序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试做一个重复的任务,我可以改变它重复的延迟。以下是我正在使用的当前代码:

I am trying to make a repeating task where I can change the delay in which it repeats. Here is the current code I am using:

var actionwait = SKAction.waitForDuration(self.wait) var actionrun = SKAction.runBlock({ self.count+=1 if (self.count % 2 == 0 && self.wait > 0.2){ self.wait -= 0.1 actionwait.duration = self.wait } for node in self.children{ if (node.isMemberOfClass(Dot)){ node.removeFromParent() } } var radius = CGFloat(arc4random_uniform(100) + 30) var newNode = Dot(circleOfRadius: radius) var color = self.getRandomColor() newNode.fillColor = color newNode.strokeColor = color newNode.yScale = 1.0 newNode.xScale = 2.0 newNode.userInteractionEnabled = true newNode.setScene(self) newNode.position = newNode.randomPos(self.view!) self.addChild(newNode) }) self.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))

但是,似乎因为序列已经重复,改变延迟的持续时间不会影响任何事情。

However, it appears that because the sequence is already repeating, changing the duration of the delay does not effect anything.

推荐答案

当你创建 actionwait 时,它会将值保存在关闭并继续在你的 repeatActionForever中使用它们

when you create actionwait it's going to save the values inside that closure and keep using them in your repeatActionForever

当你跟踪变化时,最好做那种事情在更新方法中。在这种情况下使用操作可能不是最好的方法。

When you're tracking changes it's best to do that sort of thing in the update method. Using actions in this case might not be the best approach.

我不确定你(对于你的游戏)何时需要检查更改。对于大多数使用更新方法的方法是足够的

I'm not sure about when you (for your game) need to be checking for changes. For most things using the update method is adequate

这是我在更新中实现计时器的一种方式

heres one way i implement a timer in update

// MY TIMER PROPERTIES IN MY CLASS var missleTimer:NSTimeInterval = NSTimeInterval(2) var missleInterval:NSTimeInterval = NSTimeInterval(2) // IN MY UPDATE METHOD self.missleTimer -= self.delta if self.missleTimer <= 0 { // TIMER HIT ZERO, DO SOMETHING! self.launchMissle() // LAUNCH MY MISSLE self.missleTimer = self.missleInterval // OKAY LETS RESET THE TIMER }

delta是此帧与最后一帧之间的时间差。它用于创建流体运动和跟踪时间。像这样的东西。这是spritekit项目中非常标准的。你通常需要使用delta projectwide

delta is the difference of time between this frame and the last frame. It's used create fluid motion, and track time. Things like that. This is pretty standard among spritekit projects. you usually need to use delta projectwide

声明这两个属性:

// time values var delta:NSTimeInterval = NSTimeInterval(0) var last_update_time:NSTimeInterval = NSTimeInterval(0)

这应该是更新方法的顶部看起来的方式

This should be how the top of your update method looks

func update(currentTime: NSTimeInterval) { if self.last_update_time == 0.0 self.delta = 0 } else { self.delta = currentTime - self.last_update_time } self.last_update_time = currentTime

更多推荐

运行时修改重复的任务序列

本文发布于:2023-11-23 19:13:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!