我搞了一个打字效果的Action ,但是这个action 连续跑上 三个后,
若是后续字符串比之前的长度小,必出问题 。。。。
class LabelTypeInAction : public ActionInterval
{
public:
virtual ActionInterval* reverse() const
{
CCASSERT(false, "CAN'T REVERSE");
return NULL;
};
virtual ActionInterval* clone() const
{
auto a = new LabelTypeInAction();
a->init(_duration, _NewStr);
a->autorelease();
return a;
};
~LabelTypeInAction()
{
}
static LabelTypeInAction* create(float dt, std::string aNewStr)
{
LabelTypeInAction* CM = new LabelTypeInAction();
if (CM && CM->init(dt, aNewStr))
{
CM->autorelease();
return CM;
}
else
{
CC_SAFE_FREE(CM);
return NULL;
}
}
virtual void startWithTarget(Node* pTarget)
{
// CCASSERT(typeid(pTarget) == typeid(Label*), "can't Run On other Object");
pInnerTarget = static_cast(pTarget);
pInnerTarget->setString(_NewStr);
log("_NewStr %s", _NewStr.c_str());
Size TmpSize = pInnerTarget->getContentSize();
for (int i = 0; i < pInnerTarget->getStringLength(); i++)
{
_CharSpriteList.pushBack(pInnerTarget->getLetter(i));
pInnerTarget->getLetter(i)->setOpacity(0);
}
_TotalShowCount = _duration / cocos2d::Director::getInstance()->getAnimationInterval();
_CharShowCount = (_TotalShowCount - 2) / pInnerTarget->getStringLength();
CC_ASSERT(floorf(_TotalShowCount / _CharShowCount) >= pInnerTarget->getStringLength());
_StepOpacityCount = ceilf(255.0f / _CharShowCount);
_Counter = 0.0f;
}
bool init(float dt, std::string aNewStr)
{
_NewStr = aNewStr;
ActionInterval::initWithDuration(dt);
return true;
}
virtual void update(float dt)
{
Sprite* aChareSprite = _CharSpriteList.at(getCharSprite(_Counter));
aChareSprite->setOpacity(getCharSpriteOpacity(_Counter, aChareSprite));
_Counter = _Counter + 1.0f;
}
protected:
float _TotalShowCount, _CharShowCount, _Counter;
int _StepOpacityCount;
std::string _NewStr;
Label* pInnerTarget;
Vector _CharSpriteList;
private:
// CC_DISALLOW_COPY_AND_ASSIGN(LabelTypeInAction);
int getCharSprite(float aCounter)
{
int aIndex = floorf(aCounter / _CharShowCount);
return (aIndex >= _CharSpriteList.size()) ? _CharSpriteList.size() - 1 : aIndex;
}
int getCharSpriteOpacity(float aCounter, Sprite* aChareSprite)
{
int NowOpacity = aChareSprite->getOpacity();
NowOpacity += _StepOpacityCount;
return (NowOpacity > 255 ? 255 : NowOpacity);
}
};
```