I have a character sprite which walks to different points frequently. The walking animation is a CCSpawn of CCMoveTo and CCAnimate, where CCMoveTo moves the sprite in screen and CCAnimate show walking frame sequence. It also involves some calculations to determine the time that should be taken to walk to the destination (based on Human's speed).
For the above requirement I created a custom CCAction class HAWalk inherited from CCSpawn and the code goes as follows:
class HAWalk :public CCSpawn {
/** regular functions (removed here)_ */
/** creates the Spawn action */
static HAWalk* create(CCPoint destination, CCSprite *object);
protected:
CCFiniteTimeAction *m_pOne;
CCFiniteTimeAction *m_pTwo;
float duration;
};
// destination is the point to go and object is the targetObject
HAWalk* HAWalk::create(CCPoint destination, CCSprite *object) {
float dist;
float duration;
int animCycle;
HAWalk *pSpawn;
CCFiniteTimeAction *walkAction, *pAction1, *pAction2;
dist = ccpDistance(destination, object->getPosition());
walkAction = (CCFiniteTimeAction *)Human::getActions()->objectAtIndex(HumanActionWalk);
// time taken to walk to the destination
duration = dist / WALKSPEED;
// number of times the walking frame sequence is to be shown
animCycle = ceilf(duration / walkAction->getDuration());
// duration rounded to cover integral number of walking frame cycles
duration = animCycle * walkAction->getDuration();
pAction1 = CCRepeat::create(walkAction, animCycle);
pAction2 = CCMoveTo::create(duration, destination);
pSpawn = new HAWalk();
pSpawn->initWithTwoActions(pAction1, pAction2);
pSpawn->autorelease();
return pSpawn;
}
All remaining functions are ditto copy of CCSpawn. Is this the correct approach and Is there any better way of doing this ?
I believe I dont understand the workings of CCAction the right way, since I am passing the target object with create which is not the case with other in-built actions.
Thanks :-)