Adds init(array) for Sequence and Spawn

it also makes the code cleaner.
this is needed for JS
This commit is contained in:
Ricardo Quesada 2015-12-03 12:16:21 -08:00
parent d0ce2473a1
commit 3646d1e9fc
4 changed files with 128 additions and 42 deletions

View File

@ -238,29 +238,30 @@ Sequence* Sequence::createWithVariableList(FiniteTimeAction *action1, va_list ar
Sequence* Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions)
{
Sequence* ret = nullptr;
do
Sequence* seq = new (std::nothrow) Sequence;
if (seq && seq->init(arrayOfActions))
seq->autorelease();
return seq;
}
bool Sequence::init(const Vector<FiniteTimeAction*>& arrayOfActions)
{
auto count = arrayOfActions.size();
if (count == 0)
return true;
if (count == 1)
return initWithTwoActions(arrayOfActions.at(0), ExtraAction::create());
// else size > 1
auto prev = arrayOfActions.at(0);
for (int i = 1; i < count-1; ++i)
{
auto count = arrayOfActions.size();
CC_BREAK_IF(count == 0);
prev = createWithTwoActions(prev, arrayOfActions.at(i));
}
auto prev = arrayOfActions.at(0);
if (count > 1)
{
for (int i = 1; i < count; ++i)
{
prev = createWithTwoActions(prev, arrayOfActions.at(i));
}
}
else
{
// If only one action is added to Sequence, make up a Sequence by adding a simplest finite time action.
prev = createWithTwoActions(prev, ExtraAction::create());
}
ret = static_cast<Sequence*>(prev);
}while (0);
return ret;
return initWithTwoActions(prev, arrayOfActions.at(count-1));
}
bool Sequence::initWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo)
@ -633,27 +634,10 @@ Spawn* Spawn::createWithVariableList(FiniteTimeAction *action1, va_list args)
Spawn* Spawn::create(const Vector<FiniteTimeAction*>& arrayOfActions)
{
Spawn* ret = nullptr;
do
{
auto count = arrayOfActions.size();
CC_BREAK_IF(count == 0);
auto prev = arrayOfActions.at(0);
if (count > 1)
{
for (int i = 1; i < arrayOfActions.size(); ++i)
{
prev = createWithTwoActions(prev, arrayOfActions.at(i));
}
}
else
{
// If only one action is added to Spawn, make up a Spawn by adding a simplest finite time action.
prev = createWithTwoActions(prev, ExtraAction::create());
}
ret = static_cast<Spawn*>(prev);
}while (0);
Spawn* ret = new (std::nothrow) Spawn;
if (ret && ret->init(arrayOfActions))
ret->autorelease();
return ret;
}
@ -666,6 +650,26 @@ Spawn* Spawn::createWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *
return spawn;
}
bool Spawn::init(const Vector<FiniteTimeAction*>& arrayOfActions)
{
auto count = arrayOfActions.size();
if (count == 0)
return true;
if (count == 1)
return initWithTwoActions(arrayOfActions.at(0), ExtraAction::create());
// else count > 1
auto prev = arrayOfActions.at(0);
for (int i = 1; i < count-1; ++i)
{
prev = createWithTwoActions(prev, arrayOfActions.at(i));
}
return initWithTwoActions(prev, arrayOfActions.at(count-1));
}
bool Spawn::initWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2)
{
CCASSERT(action1 != nullptr, "action1 can't be nullptr!");

View File

@ -192,6 +192,7 @@ CC_CONSTRUCTOR_ACCESS:
/** initializes the action */
bool initWithTwoActions(FiniteTimeAction *pActionOne, FiniteTimeAction *pActionTwo);
bool init(const Vector<FiniteTimeAction*>& arrayOfActions);
protected:
FiniteTimeAction *_actions[2];
@ -417,6 +418,7 @@ CC_CONSTRUCTOR_ACCESS:
/** initializes the Spawn action with the 2 actions to spawn */
bool initWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2);
bool init(const Vector<FiniteTimeAction*>& arrayOfActions);
protected:
FiniteTimeAction *_one;

View File

@ -35,6 +35,9 @@ USING_NS_CC;
ActionsTests::ActionsTests()
{
ADD_TEST_CASE(ActionSequence3);
ADD_TEST_CASE(ActionSpawn2);
ADD_TEST_CASE(ActionMove);
ADD_TEST_CASE(ActionMove3D);
ADD_TEST_CASE(ActionRotate);
@ -54,7 +57,8 @@ ActionsTests::ActionsTests()
ADD_TEST_CASE(ActionAnimate);
ADD_TEST_CASE(ActionSequence);
ADD_TEST_CASE(ActionSequence2);
ADD_TEST_CASE(ActionRemoveSelf);
ADD_TEST_CASE(ActionSequence3);
ADD_TEST_CASE(ActionRemoveSelf);
ADD_TEST_CASE(ActionSpawn);
ADD_TEST_CASE(ActionReverse);
ADD_TEST_CASE(ActionDelayTime);
@ -776,6 +780,37 @@ std::string ActionSequence2::subtitle() const
return "Sequence of InstantActions";
}
//------------------------------------------------------------------
//
// ActionSequence3
//
//------------------------------------------------------------------
void ActionSequence3::onEnter()
{
ActionsDemo::onEnter();
alignSpritesLeft(1);
// Uses Array API
auto action1 = MoveBy::create(2, Vec2(240,0));
auto action2 = RotateBy::create(2, 540);
auto action3 = action1->reverse();
auto action4 = action2->reverse();
Vector<FiniteTimeAction*> array;
array.pushBack(action1);
array.pushBack(action2);
array.pushBack(action3);
array.pushBack(action4);
auto action = Sequence::create(array);
_grossini->runAction(action);
}
std::string ActionSequence3::subtitle() const
{
return "Sequence: Using Array API";
}
//------------------------------------------------------------------
//
// ActionCallFuncN
@ -947,6 +982,33 @@ std::string ActionSpawn::subtitle() const
return "Spawn: Jump + Rotate";
}
//------------------------------------------------------------------
//
// ActionSpawn2
//
//------------------------------------------------------------------
void ActionSpawn2::onEnter()
{
ActionsDemo::onEnter();
alignSpritesLeft(1);
auto action1 = JumpBy::create(2, Vec2(300,0), 50, 4);
auto action2 = RotateBy::create( 2, 720);
Vector<FiniteTimeAction*> array;
array.pushBack(action1);
array.pushBack(action2);
auto action = Spawn::create(array);
_grossini->runAction(action);
}
std::string ActionSpawn2::subtitle() const
{
return "Spawn: using the Array API";
}
//------------------------------------------------------------------
//

View File

@ -207,6 +207,15 @@ public:
void callback3(Node* sender, long data);
};
class ActionSequence3 : public ActionsDemo
{
public:
CREATE_FUNC(ActionSequence3);
virtual void onEnter() override;
virtual std::string subtitle() const override;
};
class ActionSpawn : public ActionsDemo
{
public:
@ -216,6 +225,15 @@ public:
virtual std::string subtitle() const override;
};
class ActionSpawn2 : public ActionsDemo
{
public:
CREATE_FUNC(ActionSpawn2);
virtual void onEnter() override;
virtual std::string subtitle() const override;
};
class ActionReverse : public ActionsDemo
{
public: