Custom Actions

Custom Actions
0.0 0


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 _/
/* creates the Spawn action/
static HAWalk* create(CCPoint destination, CCSprite object);
CCFiniteTimeAction m_pTwo;
float duration;
// destination is the point to go and object is the targetObject
HAWalk::create(CCPoint destination, CCSprite object) {
float dist;
float duration;
int animCycle;
CCFiniteTimeAction **walkAction,**pAction1, *pAction2;
dist = ccpDistance(destination, object~~>getPosition);
walkAction = Human::getActions~~>objectAtIndex;
// time taken to walk to the destination
duration = dist / WALKSPEED;
// number of times the walking frame sequence is to be shown
animCycle = ceilf);
// duration rounded to cover integral number of walking frame cycles
duration = animCycle * walkAction~~>getDuration;
pAction1 = CCRepeat::create;
pAction2 = CCMoveTo::create;
pSpawn = new HAWalk;
pSpawn~~>initWithTwoActions(pAction1, pAction2);
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.
PS: Posted the same @
Thanks :~~)


So, what’s the problem here? Unwanted ‘object’ parameter?

Calculate duration outside of create() as you would with other Interval actions, and pass
it as parameter instead of object.


There is no problem, I just want to make sure if this is the right method to create custom CCAction.
Also I prefer to have the duration calculation inside the `create()` as it reduced a lot of code duplication when outside the call.