我试图用cocos2d-x制作动画序列。我想一个接一个地向下移动精灵。
这是我的第一次尝试:
auto fallAction = MoveBy::create(0.2f, Vec2(0, -director->getWinSize().height));
auto fallActionEase = EaseIn::create(fallAction, 2.0f);
auto fallStretch = ScaleBy::create(0.1f, 1.0f, 1.2f);
auto fall = Spawn::create(fallActionEase, fallStretch, NULL);
auto landTremble = EaseElasticOut::create(ScaleTo::create(0.5f, _finalScale));
this->getK()->runAction(Sequence::create(Delay::create(0.5f), fall, landTremble));
this->getA()->runAction(Sequence::create(Delay::create(1.0f), fall, landTremble));
this->getW()->runAction(Sequence::create(Delay::create(1.5f), fall, landTremble));
但它没有工作,正如[这里](Reuse cocos2d actions)所讨论的那样。
然后我发现I can copy actions,但后来我发现Clonable :: copy()现在已被弃用(似乎它甚至不存在于v3.6中!)
我最终筑巢了lambdas,如here(第246至254行)。
我想重复使用' actions
以良好的方式!我想要实现的目标如下:
creating
只有一个 Action
来确定精灵的移动编辑:为防止链接损坏,我将代码粘贴到嵌套lambdas的位置。不太聪明。
auto DesiredAction = Sequence::create(wait4Frog, Fall, Spawn::create(FallSound, LandTremble,CallFunc::create([this, Fall, LandTremble](){
this->getA1()->runAction(Sequence::create(Fall, Spawn::create(LandTremble,CallFunc::create([this, Fall, LandTremble](){
this->getW()->runAction(Sequence::create(Fall, Spawn::create(LandTremble,CallFunc::create([this, Fall, LandTremble](){
this->getA2()->runAction(Sequence::create(Fall, Spawn::create(LandTremble,CallFunc::create([this, Fall, LandTremble](){
this->getZ()->runAction(Sequence::create(Fall, LandTremble, NULL));
}), NULL), NULL));
}), NULL), NULL));
}), NULL), NULL));
}), NULL), NULL);
答案 0 :(得分:2)
这个怎么样:
Action* createSequence(float delay)
{
auto fall = Spawn::create(
EaseIn::create(MoveBy::create(0.2f, Vec2(0, -director->getWinSize().height)), 2.0f),
ScaleBy::create(0.1f, 1.0f, 1.2f),
NULL
);
auto landTremble = EaseElasticOut::create((ScaleTo::create(0.5f, _finalScale)));
return Sequence::create(Delay::create(delay), fall, landTremble, NULL);
}
然后你可以使用这个功能:
this->getK()->runAction(createSequence(0.5f));
this->getA()->runAction(createSequence(1.f));
this->getW()->runAction(createSequence(1.5f));
答案 1 :(得分:1)
如上所述,您可以将动作的定义封装到函数中,并将变量作为参数。然后重用该函数。
如果你每次只需要完全相同的动作,还有另一种方法。您可以创建一个操作,保留它,然后每次使用
someNode -> runAction( action->clone() );
保留动作,最后需要释放。