cocos2d-xccaction

cocos2d :: CCmoveTO


Hi, i am new to cocos2dx programming........

I am trying to make a projectile motion of a target sprite

// in addtarget() part

int i(0);
while(i<20)
{
p=HelloWorld::nextPoint(p,0.5);//p is a struct see below.

cocos2d::CCFiniteTimeAction* actionMove=cocos2d::CCMoveTo::actionWithDuration(1.0, ccp(p.x,p.y));
target->runAction( cocos2d::CCSequence::actions(actionMove,NULL));
++i;
}
cocos2d::CCFiniteTimeAction* actionMoveDone = cocos2d::CCCallFuncN::actionWithTarget( this,callfuncN_selector(HelloWorld::spriteMoveFinished));

target->runAction( cocos2d::CCSequence::actions(actionMoveDone, NULL) );

nextPoint is a function which returns struct p(which have time,velocity and space coordinates t,x,y,vx,vy) When outside the loop, it runs all fine but when inside it doesn't show anything debugging done and all cordinates are fine Tried removing NUll can't do Finally using bezier so now it works partially. But unable to understand why CCMoveTo doesn't work. it always showed initial coordinates of (60,60) & also some good tutorial link for cocos 2dx for people who r new to mobile game programming. Thanks!


Solution

  • I am not completely sure that it makes sense running multiple MoveTo actions on the same target. Indeed, you are saying to the same target to move at different locations at the same time, this cannot produce any reliable result.

    You could try using MoveBy, where you specify an incremental displacement, but most probably you will need to rework the logic of your code, if what you want is that you target follows a given trajectory. In this case, you could create a larger sequence containing all of the MoveTo actions you need to run. E.g. (in pseudo code):

     cocos2d::CCSequence* actionSequence = <create empty sequence>;
     while(i<20)
     {
         p = HelloWorld::nextPoint(p,0.5);
         cocos2d::CCFiniteTimeAction* actionMove = ...
         <add actionMove to actionSequence>
         ++i;
     }
    
     target->runAction(actionSequence);