I'm attempting to have a character move across the screen from point A to point B on user touch.
I'm currently doing this with SKActions (in a group). However, I've noticed that SKActions take a duration, so there will be not constant movement speed which is a deal breaker. Closer distances will cause the character to move slower while far way distances make the character move faster.
Is there a better way for doing this? I was thinking in using the -update method in the scene but not sure what would be the best way to tie this into the touch events.
Any recommendations?
All you need to do is calculate the duration yourself using the distance and speed.
speed = distance/time, time being your duration, so solve for t.
Using some pseduo code here:
function moveToWithSpeed(p1, endPoint: p2, speed: speed)
{
//credit: http://stackoverflow.com/questions/1906511/how-to-find-the-distance-between-two-cg-points
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));
duration = distance/speed
SKAction.moveTo(p2, duration: duration);
}
The rest I think you can figure out yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With