CCMoveTo at a constant speed

CCMoveTo at a constant speed
0.0 0


I need a sprite to move at a constant speed and I figured I could use CCMoveTo for this, however, it takes a duration parameter. Of course, I can figure out what the duration should be by determining the distance between the destination and the starting point and how fast it should be traveling.

However, I wasn’t sure if this already existed with cocos2d. I’ve checked around and the only resources I could find suggested doing as I stated. The thing is, I might as well write my own function that modifies the sprites position:
x += cos(direction) * speed;
y -= sin(direction) * speed;

So I am just wondering if there was a way to use CCMoveTo to move to a point at a constant speed, instead of over a duration.


Think you’d be better off using delta (as in “OnFrame( ccTime delta )”).

delta is the time since the last frame, so say you want to move at 3 points per second:

void yourApp::OnFrame( ccTime delta )
delta = delta * 60; // there are 60 frames in a second
CCPoint Pos = sprite~~>getPosition;
Pos.x += ; // move 3 points per frame
sprite~~>setPosition( Pos );


So you’re basically confirming what I said about modifying the position myself instead of using a CCMoveTo action?

The thing is, wouldn’t it make sense to add a CCMoveTo action that takes speed into account instead of how long the action should take?


As far as I know, of course you can do the other option you already thought of and calculate the duration yourself like:

CCPoint start = sprite->getPosition();
CCPoint end = ccp(x,y);
float distance = ccpDistance(start, end);
float duration = distance/speed;

… use your calculated duration in your action


Is there a reason that don’t have a version of CCMoveTo that uses a speed instead of a duration? Just curious.


It’s design by cocos2d-iphone. I think it’s right. Speed * Duration = distance, and then convert distance to coordinate in pixels, the result hard to be an integer.