Sequencing actions across multiple sprites by using cocos2d-x

There is a class called TargetedAction ( Author: Karl Stenerud http://www.mindsnacks.com/ ) which is quite handy for sequencing actions across multiple sprites. This class has been integrated into cocos2d ver1.1 as well
[[https://github.com/cocos2d/cocos2d-iphone/commit/27164b3f56ba5a9f4592165ad1111587645a0f40]]

As I haven’t found it in cocos2d-x lib so far, so just converted into cpp version.

hope this is helpful to someone who had the same issue.

Here is the converted class:

//
// TargetedAction.h
//
// Created by Matt Wang on 29 Feb 2012.
// DostSoft Ltd, http://Mattcn.com
//
// original auther: Karl Stenerud http://www.mindsnacks.com/
// reference:https://github.com/cocos2d/cocos2d-iphone/commit/27164b3f56ba5a9f4592165ad1111587645a0f40
//
#include “cocos2d.h”
//
/* Overrides the target of an action so that it always runs on the target
** specified at action creation rather than the one specified by runAction.
/
class TargetedAction : public cocos2d::CCActionInterval //
{
public:
cocos2d::CCFiniteTimeAction
action;
/
* This is the target that the action will be forced to run with**/
CC_SYNTHESIZE_RETAIN(cocos2d::CCNode**, forcedTarget, ForcedTarget);
/* Create an action with the specified action and forced target /
static cocos2d::CCActionInterval
actionWithTarget(cocos2d::CCNode* target, cocos2d::CCFiniteTimeAction* action);
private:
/
* Init an action with the specified action and forced target**/
bool init(cocos2d::CCNode* target, cocos2d::CCFiniteTimeAction* action);
// cocos2d::CCActionInterval* initByAnotherName(cocos2d::CCNode* targetIn, cocos2d::CCFiniteTimeAction* actionIn);
void startWithTarget(cocos2d::CCNode* aTarget);
void stop();
void update(cocos2d::ccTime time);
protected:
TargetedAction;\
\ };

\ #include\ “TargetedAction.h”
\ using\ namespace\ cocos2d;
\ CCActionInterval*\ TargetedAction::actionWithTarget\ {
\ TargetedAction*\ obj\ =\ new\ TargetedAction;
\ obj~~>init;
\ return\ obj;
\ }\
\ //
\ /*\ Init\ an\ action\ with\ the\ specified\ action\ and\ forced\ target/
\ bool\ TargetedAction::init\ {
\ this~~>initWithDuration);
\ targetIn~~>retain;
\ forcedTarget\ =\ targetIn;
\ actionIn~~>retain;
\ action\ =\ actionIn;
\ return\ true;
\ }
\ //
\ TargetedAction::TargetedAction() {
CC_SAFE_RELEASE(this~~>forcedTarget);
CC_SAFE_RELEASE;
}
//
void TargetedAction::startWithTarget {
action~~>startWithTarget(forcedTarget);
}
//
void TargetedAction::stop()
{
action~~>stop;
}
//
void TargetedAction::update
{
action~~>update(time);
}

Example:

CFiniteTimeAction* sequence = CCSequence::actions( CCSpawn::actions(
TargetedAction::actionWithTarget(this~~>obj1, action1),
TargetedAction::actionWithTarget,
NULL),
CCSequence::actions,
TargetedAction::actionWithTarget,
NULL),
CCCallFunc::actionWithTarget),
NULL);
this~~>runAction(sequence);

please correct me if i wrote something incorrect.

Cheers,

mattcn


TargetedAction.h.zip (0.9 KB)