CCProgressTo :
class CC_DLL CCProgressTo : public CCActionInterval
{
public:
/** Initializes with a duration and a percent */
bool initWithDuration(float duration, float fPercent);
virtual CCObject* copyWithZone(CCZone *pZone);
virtual void startWithTarget(CCNode *pTarget);
virtual void update(float time);
public:
/** Creates and initializes with a duration and a percent */
static CCProgressTo* create(float duration, float fPercent);
protected:
float m_fTo;
float m_fFrom;
};
这是Cocos2d-x里面一个特殊的动作。看名字就知道其作用了。
可是在2.1.4版本里有个做血条的Bug。
一般情况下,我们在战斗的回调函数里面调用如下代码,进行血条的更新:
CCProgressTo *to = CCProgressTo::create(2, percent); // percent每次都会更新
bar->runAction(to);
percent为100的时候,动作会从0百分比到100百分比。
源码如下:
void CCProgressTo::startWithTarget(CCNode *pTarget)
{
CCActionInterval::startWithTarget(pTarget);
m_fFrom = ((kProgressTimerCast)(pTarget))->getPercentage();
// XXX: Is this correct ?
// Adding it to support CCRepeat
if (m_fFrom == 100)
{
m_fFrom = 0;
}
}
源码中自己还注释了,it to support CCRepeat...也就是说,这个动作如果是to100百分比的话,可以循环播放。
所以导致了在游戏中头两次血条更新时的不正常的问题。
根据不改引擎源码的原则,修改建议如下:
bar->setPercentage(99.999f); // 初始化
