我想将动画精灵与路径一起移动(我的意思是std :: vector 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);  }


但我发现我的代码有问题。精灵在没有动画的情况下移动。

有人可以告诉我为什么并为我找到解决方案吗?
谢谢你们

最佳答案

您的代码还不完整,但是似乎您应该将动画动作和运动动作分开。它们可以同时运行。尝试以下伪代码:

//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/

10-11 22:35
查看更多