Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move actions while animating

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?

like image 771
MrShoot Avatar asked Aug 14 '26 19:08

MrShoot


1 Answers

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.

like image 145
AwDogsGo2Heaven Avatar answered Aug 17 '26 08:08

AwDogsGo2Heaven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!