我想移动一个动画精灵和一条路径(我的意思是std :: vector< cocos2d :: Point> st)。
这是我的代码。
std::vector<cocos2d::Point> st;
Vector< FiniteTimeAction * > fta;
.......
while (!st.empty()) {
auto des = st.back();
auto moveAction = MoveTo::create(des.distance(currentPos) / 34, des);
...
auto aniForever = RepeatForever::create(moveAnimation);
auto seq = Sequence::create(moveAction, CallFunc::create(CC_CALLBACK_0(Barbarian::stopAnimation, this, aniForever)), NULL);
auto spw = Spawn::create(aniForever, seq, NULL);
fta.pushBack(spw);
.....
st.pop_back();
}
auto seq = Sequence::create(fta);
sprite->runAction(seq);
这样,当moveAction完成时,它将调用stopAnimation来停止aniForever。 stopAnimation在
之下void CharacterBase::stopAnimation(cocos2d::RepeatForever *ani) {
CCLOG("STOP ANIMATION");
sprite->stopAction(ani); }
但我发现我的代码有些问题。精灵在没有动画的情况下移动。
有人可以告诉我为什么并为我找到解决方案吗? 谢谢大家
答案 0 :(得分:1)
您的代码不完整,但似乎您应该将动画操作和移动操作分开。它们可以同时运行。试试这个伪代码:
//when you start moving
auto moveAnimation = createMoveAnimation();
sprite->runAction(moveAnimation);//start play move animation now
Vector< FiniteTimeAction * > fta;
while (!st.empty()) {
auto des = st.back();
auto moveAction = MoveTo::create(des.distance(currentPos) / 34, des);
fta.pushBack(moveAction );
st.pop_back();
}
auto endAnimation = CallFunc::create(CC_CALLBACK_0(Barbarian::stopAnimation, this, aniForever)//after movement stop movement animation
fta.pushBack(endAnimation);
//so far, you have created a list of moving actions and a stop animation call at the end
auto seq = Sequence::create(fta);
sprite->runAction(seq);//let the sprite move