我有一个使用for循环在SKScene周围移动矩形的函数。有没有办法让它在调用SKAction时一直移动矩形?它等待运行SKAction直到循环结束,这不是我想要做的。
以下是我的代码示例:
for i in 0...newHeights.count-1 {
for j in stride(from: 0, to: newHeights.count-i-1, by: 1){
let moveRect = SKAction.moveTo(x: rectangles[j].position.x, duration: 5)
moveRect.timingMode = SKActionTimingMode.easeInEaseOut
rectangles[j+1].run(moveRect)
let sequence = SKAction.sequence([moveRect])
rectangles[j+1].run(sequence)
}
}
编辑:现在该部分正在运行,但它以意想不到的方式移动。我可以通过
来切换位置rectangles[j].position.x = rectangles[j+1].position.x
rectangles[j+1].position.x = y
但是当我尝试使用SKActions移动它们时,它会以完全不同的方式移动它们。
let moveRect = SKAction.moveTo(x: rectangles[j+1].position.x, duration: 1)
//moveRect.timingMode = SKActionTimingMode.easeInEaseOut
let waitAction = SKAction.wait(forDuration: 2)
let sequence = SKAction.sequence([moveRect, waitAction])
let moveRect2 = SKAction.moveTo(x: y, duration: 1)
moveRect2.timingMode = SKActionTimingMode.easeInEaseOut
let waitAction2 = SKAction.wait(forDuration: 2)
let sequence2 = SKAction.sequence([moveRect2, waitAction2])
rectangles[j].run(sequence) {
self.rectangles[j+1].run(sequence2)
}
答案 0 :(得分:0)
如评论中所述,将速度设置为0,然后在当前节点完成时打开下一个节点的速度
唯一的问题,0会发生什么?这是故意空白吗?
for i in 0..<newHeights.count {
for j in stride(from: 0, to: newHeights.count-i-1, by: 1){
let moveRect = SKAction.moveTo(x: rectangles[j].position.x, duration: 5)
let startNextRect = SKAction.run{if (j+2 < newHeights.count){self.rectangles[j+2].speed = 1}}
moveRect.timingMode = SKActionTimingMode.easeInEaseOut
let seq = SKAction.sequence([moveRect,startNextRect])
rectangles[j+1].run(seq)
rectangles[j+1].speed = 0
}
}
rectangles[1].speed = 1