cocos2d-x通过扩展自己的Action,简化一些代码

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是什么?是练习,你们可以自己写写。保证精灵在移动时总自动保持一定的遮挡关系。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值