[英]SKAction wait interval NOT consistent
I have a function that creates SpriteNode copies and move each copy along a rectangle path at a specified interval. 我有一个函数创建SpriteNode副本并以指定的间隔沿矩形路径移动每个副本。 I have a problem with this interval that the action is executed at inconsistent interval making odd gaps between sprite nodes while action is running.
我对此间隔有一个问题,即操作以不一致的间隔执行,在操作运行时在精灵节点之间产生奇怪的间隙。
var items = [SKSpriteNode]();
let item = SKSpriteNode(imageNamed: "Spaceship");
func createItem(scene: SKScene) {
let square = UIBezierPath(rect: CGRect(x: -300, y: -150, width: 700, height: 300));
let follow = SKAction.follow(square.cgPath, asOffset: false, orientToPath: false, duration: 5.0);
for i in 0..<6 {
if let itemCopy = item.copy() as? SKSpriteNode {
itemCopy.position = CGPoint(x: -300, y: -150);
items.append(itemCopy);
scene.addChild(itemCopy);
let otherWait = SKAction.wait(forDuration: 1.0, withRange: 2.0);
let otherSequence = SKAction.sequence([otherWait, SKAction.repeatForever(follow)]);
itemCopy.run(otherSequence);
}
}
}
My understanding is that I can't rely on for loop
for timing and this issue could be caused by that. 我的理解是我不能依赖
for loop
来定时,这个问题可能是由此造成的。 Is there a way to get around this with some kind of callback function? 有没有办法通过某种回调函数解决这个问题?
The problem here is not related to the for loop, but comes from that fact you're using wait(forDuration:withRange:)
, and this is the intended behaviour. 这里的问题与for循环无关,但是来自你正在使用
wait(forDuration:withRange:)
事实,这是预期的行为。 The documentation states: 文件说明:
Creates an action that idles for a randomized period of time.
创建一个随机时间段空闲的动作。
I think you're looking for the wait(forDuration:)
method and it will work just fine :) 我想你正在寻找
wait(forDuration:)
方法,它会工作得很好:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.