#ifndef __CCINSTANT_ACTION_H__
#define __CCINSTANT_ACTION_H__
#include <string>
#include "ccTypeInfo.h"
#include "CCAction.h"
NS_CC_BEGIN
/**
@brief Instant actions are immediate(立即的 直接的) actions. They don't have a duration like
the CCIntervalAction actions.
*/
class CC_DLL CCActionInstant : public CCFiniteTimeAction //<NSCopying>继承自有限时间动作
{
public:
CCActionInstant();
virtual ~CCActionInstant(){}
// CCAction methods
virtual CCObject* copyWithZone(CCZone *pZone);
virtual bool isDone(void);//动作是否结束
virtual void step(float dt);
virtual void update(float time);
//CCFiniteTimeAction method
virtual CCFiniteTimeAction * reverse(void);
};
/** @brief Show the node
*/
class CC_DLL CCShow : public CCActionInstant
{
public:
CCShow(){}
virtual ~CCShow(){}
//super methods
virtual void update(float time);
//
void CCShow::update(float time) {
CC_UNUSED_PARAM(time);
m_pTarget->setVisible(true);
}
//virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
public:
/** Allocates and initializes the action */
static CCShow * create();
};
/**
@brief Hide the node
*/
class CC_DLL CCHide : public CCActionInstant
{
public:
CCHide(){}
virtual ~CCHide(){}
//super methods
virtual void update(float time);
//
void CCHide::update(float time) {
CC_UNUSED_PARAM(time);
m_pTarget->setVisible(false);
}
//
virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
public:
/** Allocates and initializes the action */
static CCHide * create();
};
/** @brief Toggles the visibility of a node
*/
class CC_DLL CCToggleVisibility : public CCActionInstant //Toggle 开关
{
public:
CCToggleVisibility(){}
virtual ~CCToggleVisibility(){}
//super method
virtual void update(float time);
//
void CCToggleVisibility::update(float time)
{
CC_UNUSED_PARAM(time);
m_pTarget->setVisible(!m_pTarget->isVisible());
}
//
virtual CCObject* copyWithZone(CCZone *pZone);
public:
/** Allocates and initializes the action */
static CCToggleVisibility * create();
};
/**
@brief Remove the node
*/
class CC_DLL CCRemoveSelf : public CCActionInstant //删除自己
{
public:
CCRemoveSelf(){}
virtual ~CCRemoveSelf(){}
//super methods
virtual void update(float time);
//
void CCRemoveSelf::update(float time) {
CC_UNUSED_PARAM(time);
m_pTarget->removeFromParentAndCleanup(m_bIsNeedCleanUp);
/**
* Removes this node itself from its parent node.
* If the node orphan, then nothing happens.
* @param cleanup true if all actions and callbacks on this node should be removed,
false otherwise.
*/
}
//
virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
public:
/** create the action */
static CCRemoveSelf * create(bool isNeedCleanUp = true);
/** init the action */
bool init(bool isNeedCleanUp); // 是否删除对象的actions and callbacks
protected:
bool m_bIsNeedCleanUp;
};
/**
@brief Flips the sprite horizontally
@since v0.99.0
*/
class CC_DLL CCFlipX : public CCActionInstant //Flip 弹 筋斗(翻转)
{
public:
CCFlipX()
:m_bFlipX(false)
{}
virtual ~CCFlipX(){}
/** create the action */
static CCFlipX * create(bool x);
/** init the action */
bool initWithFlipX(bool x);
//super methods
virtual void update(float time);
//
void CCFlipX::update(float time) {
CC_UNUSED_PARAM(time);
((CCSprite*) (m_pTarget))->setFlipX(m_bFlipX);
}
virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
protected:
bool m_bFlipX;
};
class CC_DLL CCFlipY : public CCActionInstant
{
public:
CCFlipY()
:m_bFlipY(false)
{}
virtual ~CCFlipY(){}
/** create the action */
static CCFlipY * create(bool y);
/** init the action */
bool initWithFlipY(bool y);
//super methods
virtual void update(float time);
virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
protected:
bool m_bFlipY;
};
/** @brief Places the node in a certain position
*/
class CC_DLL CCPlace : public CCActionInstant//瞬移动作
{
public:
CCPlace(){}
virtual ~CCPlace(){}
/** creates a Place action with a position */
static CCPlace * create(const CCPoint& pos);
/** Initializes a Place action with a position */
bool initWithPosition(const CCPoint& pos);
//super methods
virtual void update(float time);
//
void CCPlace::update(float time) {
CC_UNUSED_PARAM(time);
m_pTarget->setPosition(m_tPosition);
}
//virtual CCObject* copyWithZone(CCZone *pZone);
protected:
CCPoint m_tPosition;
};
/** @brief Calls a 'callback'
*/
class CC_DLL CCCallFunc : public CCActionInstant //<NSCopying>
{
public:
CCCallFunc()
: m_pSelectorTarget(NULL)
, m_nScriptHandler(0)
, m_pCallFunc(NULL)
{
}
virtual ~CCCallFunc();
//
CCCallFunc::~CCCallFunc(void)
{
if (m_nScriptHandler)
{
cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptHandler);
}
CC_SAFE_RELEASE(m_pSelectorTarget);
}
//
/** creates the action with the callback
typedef void (CCObject::*SEL_CallFunc)();
*/
static CCCallFunc * create(CCObject* pSelectorTarget, SEL_CallFunc selector);
/** creates the action with the handler script function */
static CCCallFunc * create(int nHandler);
/** initializes the action with the callback
typedef void (CCObject::*SEL_CallFunc)();
*/
virtual bool initWithTarget(CCObject* pSelectorTarget);
/** executes the callback */
virtual void execute(); //execute 执行
//
void CCCallFunc::execute() {
if (m_pCallFunc) {
(m_pSelectorTarget->*m_pCallFunc)();
}
if (m_nScriptHandler) {
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this);
}
}
//
//super methods
virtual void update(float time);
//
void CCCallFunc::update(float time) {
CC_UNUSED_PARAM(time);
this->execute();
}
//
CCObject * copyWithZone(CCZone *pZone);
inline CCObject* getTargetCallback()
{
return m_pSelectorTarget;
}
inline void setTargetCallback(CCObject* pSel)
{
if (pSel != m_pSelectorTarget)
{
CC_SAFE_RETAIN(pSel);
CC_SAFE_RELEASE(m_pSelectorTarget);
m_pSelectorTarget = pSel;
}
}
inline int getScriptHandler() { return m_nScriptHandler; };
protected:
/** Target that will be called */
CCObject* m_pSelectorTarget;
int m_nScriptHandler;
union
{
SEL_CallFunc m_pCallFunc;
SEL_CallFuncN m_pCallFuncN;
SEL_CallFuncND m_pCallFuncND;
SEL_CallFuncO m_pCallFuncO;
};
};
/**
@brief Calls a 'callback' with the node as the first argument
N means Node
*/
class CC_DLL CCCallFuncN : public CCCallFunc, public TypeInfo
{
public:
CCCallFuncN(){}
virtual ~CCCallFuncN(){}
virtual long getClassTypeInfo() {
static const long id = cocos2d::getHashCodeByString(typeid(cocos2d::CCCallFunc).name());
return id;
}
/** creates the action with the callback
typedef void (CCObject::*SEL_CallFuncN)(CCNode*);
*/
static CCCallFuncN * create(CCObject* pSelectorTarget, SEL_CallFuncN selector);
/** creates the action with the handler script function */
static CCCallFuncN * create(int nHandler);
/** initializes the action with the callback
typedef void (CCObject::*SEL_CallFuncN)(CCNode*);
*/
virtual bool initWithTarget(CCObject* pSelectorTarget, SEL_CallFuncN selector);
// super methods
virtual CCObject* copyWithZone(CCZone *pZone);
virtual void execute();
};
/**
@brief Calls a 'callback' with the node as the first argument and the 2nd argument is data
* ND means: Node and Data. Data is void *, so it could be anything.
*/
class CC_DLL CCCallFuncND : public CCCallFuncN
{
public:
virtual long getClassTypeInfo() {
static const long id = cocos2d::getHashCodeByString(typeid(cocos2d::CCCallFunc).name());
return id;
}
/** creates the action with the callback and the data to pass as an argument */
static CCCallFuncND * create(CCObject* pSelectorTarget, SEL_CallFuncND selector, void* d);
/** initializes the action with the callback and the data to pass as an argument */
virtual bool initWithTarget(CCObject* pSelectorTarget, SEL_CallFuncND selector, void* d);
// super methods
virtual CCObject* copyWithZone(CCZone *pZone);
virtual void execute();
protected:
void *m_pData;
};
/**
@brief Calls a 'callback' with an object as the first argument.
O means Object.
@since v0.99.5
*/
class CC_DLL CCCallFuncO : public CCCallFunc, public TypeInfo
{
public:
CCCallFuncO();
virtual ~CCCallFuncO();
virtual long getClassTypeInfo() {
static const long id = cocos2d::getHashCodeByString(typeid(cocos2d::CCCallFunc).name());
return id;
}
/** creates the action with the callback
typedef void (CCObject::*SEL_CallFuncO)(CCObject*);
*/
static CCCallFuncO * create(CCObject* pSelectorTarget, SEL_CallFuncO selector, CCObject* pObject);
/** initializes the action with the callback
typedef void (CCObject::*SEL_CallFuncO)(CCObject*);
*/
virtual bool initWithTarget(CCObject* pSelectorTarget, SEL_CallFuncO selector, CCObject* pObject);
// super methods
virtual CCObject* copyWithZone(CCZone *pZone);
virtual void execute();
inline CCObject* getObject()
{
return m_pObject;
}
inline void setObject(CCObject* pObj)
{
if (pObj != m_pObject)
{
CC_SAFE_RELEASE(m_pObject);
m_pObject = pObj;
CC_SAFE_RETAIN(m_pObject);
}
}
protected:
/** object to be passed as argument */
CCObject* m_pObject;
};
// end of actions group
/// @}
NS_CC_END
#endif //__CCINSTANT_ACTION_H__