我怎样才能'reuse'cocos2d::action?
How can I 'reuse' cocos2d::action?
我正在尝试使用 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中!)
我最终嵌套了 lambda,例如 here(第 246 到 254 行。)
我要'reuse'actions
好好的!我想要实现的是:
- 制作上面提到的完全相同的动画
- 还是
creating
只有一个Action
判断精灵的移动
- 易于维护的代码
编辑: 为防止 link 腐烂,我将在嵌套 lambda 的位置粘贴代码。不是很聪明。
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);
这个怎么样:
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));
正如接受的答案所提到的,您可以将动作的定义封装到一个函数中,需要将变化作为参数。然后复用函数。
如果您每次只需要完全相同的操作,还有另一种方法。您可以创建一个动作,保留它,然后每次使用
someNode -> runAction( action->clone() );
因为动作是保留的,所以最后需要释放。
我正在尝试使用 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中!)
我最终嵌套了 lambda,例如 here(第 246 到 254 行。)
我要'reuse'actions
好好的!我想要实现的是:
- 制作上面提到的完全相同的动画
- 还是
creating
只有一个Action
判断精灵的移动 - 易于维护的代码
编辑: 为防止 link 腐烂,我将在嵌套 lambda 的位置粘贴代码。不是很聪明。
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);
这个怎么样:
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));
正如接受的答案所提到的,您可以将动作的定义封装到一个函数中,需要将变化作为参数。然后复用函数。
如果您每次只需要完全相同的操作,还有另一种方法。您可以创建一个动作,保留它,然后每次使用
someNode -> runAction( action->clone() );
因为动作是保留的,所以最后需要释放。