2017-07-10 23 views
0

我正試圖用掃手的方式製作一些東西。SKAction Moveto沒有在持續時間內完成?

我從NSDate拉分鐘。

然後稍微計算弧度值,並找到一個XY位置,我的精靈應該在1分鐘內移動,

然後在下一分鐘開始時我計算下一個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:),或者是我的邏輯在這種情況下有缺陷?

+0

爲什麼你不使用rotateTo動作? – Knight0fDragon

+0

我不旋轉。它是一個必須在圓形路徑上行進的精靈。 我想我可以使它成爲一個非常大的透明精靈,在一端有一個點,但隨後會出現2個問題。 我無法旋轉精靈並將它移動到一個圓圈內,這將很難縮放。 另外,重點是我想了解Moveto中的持續時間問題作爲一個學習實驗,而不是試圖找到一種替代方法。 但是,我懷疑旋轉時間可能表現相似。 – Eivie

+0

是的,它會是相同的行爲 – Knight0fDragon

回答

1

嘗試從update函數中刪除代碼,然後使用您自己調用的函數運行它。 update被調用每一幀,所以精靈開始移動,然後下一幀被告知移動到另一個位置,所以它現在有兩個移動動作,它需要同時運行。之後的幀有3個需要運行的移動動作,等等。這可能意味着它從來沒有真正達到它的第一個預期位置,因爲其他移動動作同時影響它。

+0

嗯,我明白你的意思。但是這不會意味着我所要求的以1/60秒的速度運行的動作沒有在60 fps的時間內以1/60秒的速度完成? 我可以從更新函數中刪除代碼,並嘗試使用重複,但這意味着實現路徑。我對此猶豫不決,因爲它聽起來可能會使代碼複雜化,而且我也不確定它是否會提供我期望的發條精度。 – Eivie

+0

是的,由於動作持續時間設置爲60秒,動作沒有在幀中完成。我不知道是否有必要實施這條道路。您可以在第一個移動操作完成後運行下一個移動操作,如run(someAction,completion:{callTheActionAgain()}) – JohnV

+0

我不知道存在這樣的函數。我也會探索這個選項。 實際上,當我說我將它設置爲1/60秒運行時,我被誤認爲是 我重讀了我的代碼,實際上,我呼籲在60秒內執行60次,每秒60次。 是否會導致問題? – Eivie

相關問題