Search code examples
c++cocos2d-iphonecocos2d-x

move sprite from point to point at same speed


What I am doing is when I drag my finger on the screen, it stores the points into an array using ccTouchesMoved(CCSet* touches, CCEvent* event).

that works

Then in the update method if have it call the move method in my unit class, which moves the unit to the first point in the array then deletes it.

that works

My problem is that when I move my finger from point A to B slowly compared to when I move my finger from point A to B faster, it stores a different amount of points in the array and the movement is not the same.

I'm trying to mock a flight control type of game where it follows your path at the same speed always.

Is my logic off and I'm going about it the wrong way?


Solution

  • Define a constant, for example

    #define SPEED 50.0f
    

    Then when moving across the 2 point your store, e.g.

    CCPoint p1 = ccp(10,20);
    CCPoint p2 = ccp(40,60);
    

    When you create the Move, use

    CCMoveTo* moveAction = CCMoveTo::create(p2, ccpDistance(p2,p1)/SPEED);