简体   繁体   中英

SKAction Moveto not completing within the duration?

I am trying to make something like a clock with sweeping hands.

I pull the minutes from NSDate .

Then somewhat calculate a radian value and find an XY position where my sprite should move to in 1 min,

Then at the beginning of the next min I calculate the next XY position where my sprite should move to in that min.

so i have these codes inside the update function

//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))

Assuming i am understanding everything correctly, it should move through the segment in 1 minute, reaching the end of the segment before needing to move to the next segment.

however, my sprite never reaches the end of the segment before needing to move to the next segment. the printouts shows that it is always too slow.

is there something i am not understanding about SKAction.move(to:) , or is my logic flawed in this instance?

Try removing the code from the update function and just run it in a function that you call yourself. update gets called every frame, so the sprite starts to move, then the next frame it is told to move to another position, so it now has two move actions it needs to run at the same time. The frame after that it has 3 move actions it needs to run, etc. This could mean that it never really reaches its first intended position because of the other move actions that are affecting it at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM