CCActionTween(可以时间上连续的改变某个对象指定key对应的属性 改变方式需要自定义 cocos2dx未定义 )

来源:互联网 发布:金蝶数据交换平台 编辑:程序博客网 时间:2024/06/15 08:04

#ifndef __CCACTIONTWEEN_H__

#define __CCACTIONTWEEN_H__

 

///怎么通过精灵属性得到相应值啊   ????????

#include "CCActionInterval.h"


NS_CC_BEGIN



class CC_DLL CCActionTweenDelegate

{

public: //太不规范了 不写构造函数(虽然会隐式声明)!!!!!!!!!!!!!!!!!!!!!!!!!!

    virtual ~CCActionTweenDelegate() {}

    virtual void updateTweenAction(float value, const char* key) = 0;

};


/** CCActionTween


 CCActionTween is an action that lets you update any property of an object

//.ActionTween 是一个 action 让允许你更新任何对象的属性  

 For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:


    id modifyWidth = [CCActionTween actionWithDuration:2 key:@"width" from:200 to:300];

    [target runAction:modifyWidth];



 Another example: CCScaleTo action could be rewritten using CCPropertyAction:


    // scaleA and scaleB are equivalents

    id scaleA = [CCScaleTo actionWithDuration:2 scale:3];

    id scaleB = [CCActionTween actionWithDuration:2 key:@"scale" from:1 to:3];



 @since v0.99.2

 */

class CC_DLL CCActionTween : public CCActionInterval

{

public:

    /** creates an initializes the action with the property name (key), and the from and to(从xx到) parameters(参数). */

    static CCActionTween* create(float aDuration, const char* key, float from, float to);

    /** initializes the action with the property name (key), and the from and to parameters. */

    bool initWithDuration(float aDuration, const char* key, float from, float to);


    void startWithTarget(CCNode *pTarget);

    void update(float dt);

    CCActionInterval* reverse();


    std::string        m_strKey;

    float            m_fFrom, m_fTo;

    float            m_fDelta;

};


// end of actions group

/// @}


NS_CC_END


#endif /* __CCACTIONTWEEN_H__ */

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.cpp

void CCActionTween::startWithTarget(CCNode *pTarget)
{
    CCAssert(dynamic_cast<CCActionTweenDelegate*>(pTarget), "target must implement CCActionTweenDelegate");
    CCActionInterval::startWithTarget(pTarget);
    m_fDelta = m_fTo - m_fFrom;
}

void CCActionTween::update(float dt)
{
    dynamic_cast<CCActionTweenDelegate*>(m_pTarget)->updateTweenAction(m_fTo  - m_fDelta * (1 - dt), m_strKey.c_str());
}

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例子

 

class originalTargeted
{

public:

 originalTargeted(){ m_map["width"] = 18; }

 ~originalTargeted(){}

 map<string, int> m_map;


};

class CCActionTweenDelegate02 :public cocos2d::CCActionTweenDelegate, public cocos2d::CCNode
{
public:
 CCActionTweenDelegate02(originalTargeted* d){ ot = d; }
 virtual ~CCActionTweenDelegate02() {}
 virtual void updateTweenAction(float value, const char* key);

 originalTargeted* ot;
};

_____________________________

main()

{

 originalTargeted* test = new originalTargeted;
 CCActionTweenDelegate02*  s = new CCActionTweenDelegate02(test);

 CCActionTween* testaction = CCActionTween::create(2, "width", 1, 2);
 
 s->runAction(testaction);

 testaction->update(1); //???为什么runaction 不执行update

}

 

 

void CCActionTweenDelegate02::updateTweenAction(float value, const char* key)
{


 this->ot->m_map[key] = 12;

}

 

 

 

 

 

 

 

 

 

0 0