【发布时间】:2017-12-14 14:19:56
【问题描述】:
我正在尝试制作类似带有扫手的时钟。
我从NSDate 获取会议记录。
然后稍微计算一个弧度值并找到我的精灵应该在 1 分钟内移动到的 XY 位置,
然后在下一分钟开始时,我会计算我的精灵在该分钟内应该移动到的下一个 XY 位置。
所以我在更新函数中有这些代码
//Get Current Minute
var currentTime = NSDate();
var nowCal = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601)
var currMin = nowCal?.component(.minute, from: currentTime as Date)
//Print current minute
print(currMin)
// slicing the circle into 60 segments and getting which angle in radians the sprite should move to in the next min
var minAng = (2*CGFloat.pi)/60 * CGFloat(currMin!)
//calculating point on the circle where the sprite should move to in the next min
var newPt = CGPoint(x: (300 * cos(minAng)), y: -(300 * sin(minAng)))
//print out where the sprite currently is and where it should move to
print(hand.position)
print(newPt)
//move the sprite to the new location over 60 seconds, making sure the movement is linear
let moveHand = SKAction.move(to: newPt, duration: 60)
moveHand.timingMode = SKActionTimingMode.linear
hand.run(moveHand)
//move another placeholder sprite to the final destination instantly to visualise movment by waiting for the moving sprite.
handToBe.run(SKAction.move(to: newPt, duration: 0))
假设我正确理解所有内容,它应该在 1 分钟内通过该段,在需要移动到下一个段之前到达该段的末尾。
但是,我的精灵在需要移动到下一个片段之前从未到达片段的末尾。打印输出显示它总是太慢。
我对@987654323@ 有什么不明白的地方,还是我的逻辑在这种情况下有缺陷?
【问题讨论】:
-
你为什么不直接使用 rotateTo 动作?
-
我没有旋转。它是一个必须在圆形路径上移动的精灵。我想我可以让它成为一个非常大的透明精灵,一端有一个点,但是会有两个问题。我无法旋转精灵并将其移动一圈,而且很难缩放。另外,重点是我试图将 Moveto 中的持续时间问题理解为一个学习实验,而不是试图找到替代方法。但是我怀疑按持续时间轮换到的行为可能类似。
-
是的,这将是相同的行为
-
@Eivie
update:方法中的操作使用错误。如果update:方法每秒调用 60 次(理想情况下),那么运行持续时间为 60 秒(甚至 1 秒)的动作有什么意义?我的意思是没有if语句,所以我假设这段代码无论如何都会运行。 -
@Whirlwind 我正在使用更新方法,就像在游戏设计中一样。这不是更新的目的吗?我希望持续 1/60 秒的动作将在下一帧开始时结束,并为其提供下 1/60 秒的动作。
标签: ios swift sprite-kit skaction