cocos2d-x我们经常会需要创建一个精灵,执行一段动画(动作),最后一步是渐隐消失,其实消失后我们一般是希望把他remove掉。
开始我一般是动作系列的最后加上一个CCCallFunc,定义一个方法来remove精灵,反正很麻烦,需要定义方法。
现在我找到一种方法很容易实现此功能,还可以延伸出很多其他功能。
/**
@brief Remove the node
*/
class CCRemove : public CCActionInstant
{
public:
CCRemove(){}
virtual ~CCRemove(){}
//super methods
virtual void update(float time);
virtual CCFiniteTimeAction * reverse(void);
virtual CCObject* copyWithZone(CCZone *pZone);
public:
/** Allocates and initializes the action */
static CCRemove * create();
};
//
// Remove
//
CCRemove * CCRemove::create()
{
CCRemove *pRet = new CCRemove();
if (pRet) {
pRet->autorelease();
}
return pRet;
}
void CCRemove::update(float time) {
CC_UNUSED_PARAM(time);
m_pTarget->removeFromParentAndCleanup(false); //*****
}
CCFiniteTimeAction *CCRemove::reverse() {
CCAssert(false, "CCRemove: reverse not implemented.");
return NULL;
}
CCObject* CCRemove::copyWithZone(CCZone *pZone) {
CCZone *pNewZone = NULL;
CCRemove *pRet = NULL;
if (pZone && pZone->m_pCopyObject) {
pRet = (CCRemove*) (pZone->m_pCopyObject);
} else {
pRet = new CCRemove();
pZone = pNewZone = new CCZone(pRet);
}
CCActionInstant::copyWithZone(pZone);
CC_SAFE_DELETE(pNewZone);
return pRet;
}
怎么使用?很简单
CCCallFunc* callfunc = CCCallFuncND::create(this,callfuncND_selector(LQEllipseSprite::onMerge), card);
CCActionInterval* bezierTo = CCBezierTo::create( 0.6f, bc);
CCActionInstant* zorder = CCZorder::create();
CCActionInstant* zorder2 = CCZorderTo::create(999);
CCAction* se1 = CCSequence::create( CCSpawn::create(bezierTo, zorder, NULL), zorder2, CCDelayTime::create(1.0), CCRemove::create(), callfunc, NULL);
particle->runAction(se1);
当让后面还是有事件,那是干更重要的事情哟!
如果有人问 CCZorderTo是什么?是练习,你们可以自己写写。保证精灵在移动时总自动保持一定的遮挡关系。