簡體   English   中英

SKAction Moveto在持續時間內未完成?

[英]SKAction Moveto not completing within the duration?

我試圖用手打掃時鍾。

我從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分鍾內通過該段,到達該段的末尾,然后才需要移至下一個段。

但是,我的子畫面在需要移至下一個片段之前從未到達片段的末尾。 打印輸出表明它總是太慢。

我對SKAction.move(to:)有什么不了解的SKAction.move(to:)嗎?或者在這種情況下我的邏輯有缺陷嗎?

嘗試從update函數中刪除代碼,然后在您自稱的函數中運行它。 update在每一幀都被調用,因此精靈開始移動,然后告訴下一個幀移動到另一個位置,因此它現在有兩個移動動作需要同時運行。 此后的框架有3個需要運行的移動動作,依此類推。這可能意味着它從未真正到達其最初的預期位置,因為其他移動動作同時影響了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM