【问题标题】:moving animated sprite along with a path in cocos2dx沿着 cocos2dx 中的路径移动动画精灵
【发布时间】:2014-11-17 14:25:21
【问题描述】:

我想移动动画精灵和路径(我的意思是 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);  }

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

有人可以告诉我原因并为我找到解决方案吗? 谢谢大家

【问题讨论】:

  • spawn 是多余的,你可以在运行 seq 旁边执行 runAction(aniForever)。也请在您创建 moveAnimation 的位置发布代码。

标签: c++ animation cocos2d-x


【解决方案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

【讨论】: