我想沿着路径移动动画 Sprite (我的意思是 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。 停止动画在下面
void CharacterBase::stopAnimation(cocos2d::RepeatForever *ani) {
CCLOG("STOP ANIMATION");
sprite->stopAction(ani); }
但是我发现我的代码有问题。 Sprite 在没有动画的情况下移动。
有人可以告诉我原因并为我找到解决方案吗? 谢谢大家
最佳答案
您的代码不完整,但您似乎应该将动画 Action 和移动 Action 分开。它们可以同时运行。试试这个伪代码:
//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
关于c++ - 沿 cocos2dx 中的路径移动动画 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974847/