axmol/tests/cpp-tests/Classes/ActionManagerTest/ActionManagerTest.cpp

322 lines
8.1 KiB
C++
Raw Normal View History

#include "ActionManagerTest.h"
#include "../testResource.h"
#include "cocos2d.h"
2010-08-30 15:04:46 +08:00
enum
{
kTagNode,
kTagGrossini,
2010-08-30 15:04:46 +08:00
kTagSequence,
};
Layer* nextActionManagerAction();
Layer* backActionManagerAction();
Layer* restartActionManagerAction();
static int sceneIdx = -1;
#define MAX_LAYER 5
2010-08-30 15:04:46 +08:00
Layer* createActionManagerLayer(int nIndex)
2010-08-30 15:04:46 +08:00
{
switch(nIndex)
{
case 0: return new CrashTest();
case 1: return new LogicTest();
case 2: return new PauseTest();
case 3: return new StopActionTest();
2010-08-30 15:04:46 +08:00
case 4: return new ResumeTest();
}
2010-08-30 15:04:46 +08:00
return NULL;
2010-08-30 15:04:46 +08:00
}
Layer* nextActionManagerAction()
2010-08-30 15:04:46 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
2010-08-30 15:04:46 +08:00
auto layer = createActionManagerLayer(sceneIdx);
layer->autorelease();
2010-08-30 15:04:46 +08:00
return layer;
2010-08-30 15:04:46 +08:00
}
Layer* backActionManagerAction()
2010-08-30 15:04:46 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = createActionManagerLayer(sceneIdx);
layer->autorelease();
return layer;
2010-08-30 15:04:46 +08:00
}
Layer* restartActionManagerAction()
2010-08-30 15:04:46 +08:00
{
auto layer = createActionManagerLayer(sceneIdx);
layer->autorelease();
return layer;
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// ActionManagerTest
//
//------------------------------------------------------------------
ActionManagerTest::ActionManagerTest(void)
{
}
ActionManagerTest::~ActionManagerTest(void)
{
}
std::string ActionManagerTest::title() const
2014-02-28 23:02:22 +08:00
{
return "ActionManager Test";
}
std::string ActionManagerTest::subtitle() const
2010-08-30 15:04:46 +08:00
{
return "No title";
2010-08-30 15:04:46 +08:00
}
void ActionManagerTest::restartCallback(Ref* sender)
2010-08-30 15:04:46 +08:00
{
auto s = new ActionManagerTestScene();
s->addChild(restartActionManagerAction());
2010-08-30 15:04:46 +08:00
Director::getInstance()->replaceScene(s);
s->release();
2010-08-30 15:04:46 +08:00
}
void ActionManagerTest::nextCallback(Ref* sender)
2010-08-30 15:04:46 +08:00
{
auto s = new ActionManagerTestScene();
s->addChild( nextActionManagerAction() );
Director::getInstance()->replaceScene(s);
s->release();
2010-08-30 15:04:46 +08:00
}
void ActionManagerTest::backCallback(Ref* sender)
2010-08-30 15:04:46 +08:00
{
auto s = new ActionManagerTestScene();
s->addChild( backActionManagerAction() );
Director::getInstance()->replaceScene(s);
s->release();
2010-08-30 15:04:46 +08:00
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// Test1
//
//------------------------------------------------------------------
2010-08-30 15:04:46 +08:00
void CrashTest::onEnter()
{
ActionManagerTest::onEnter();
auto child = Sprite::create(s_pathGrossini);
child->setPosition( VisibleRect::center() );
addChild(child, 1);
//Sum of all action's duration is 1.5 second.
child->runAction(RotateBy::create(1.5f, 90));
child->runAction(Sequence::create(
DelayTime::create(1.4f),
FadeOut::create(1.1f),
NULL)
);
//After 1.5 second, self will be removed.
runAction( Sequence::create(
DelayTime::create(1.4f),
CallFunc::create( CC_CALLBACK_0(CrashTest::removeThis,this)),
NULL)
);
}
2010-08-30 15:04:46 +08:00
void CrashTest::removeThis()
{
_parent->removeChild(this, true);
nextCallback(this);
}
2014-02-28 23:02:22 +08:00
std::string CrashTest::subtitle() const
2010-08-30 15:04:46 +08:00
{
return "Test 1. Should not crash";
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// Test2
//
//------------------------------------------------------------------
void LogicTest::onEnter()
{
ActionManagerTest::onEnter();
auto grossini = Sprite::create(s_pathGrossini);
addChild(grossini, 0, 2);
grossini->setPosition(VisibleRect::center());
grossini->runAction( Sequence::create(
MoveBy::create(1, Vector2(150,0)),
CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)),
NULL)
);
}
void LogicTest::bugMe(Node* node)
2010-08-30 15:04:46 +08:00
{
node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
node->runAction(ScaleTo::create(2, 2));
}
2014-02-28 23:02:22 +08:00
std::string LogicTest::subtitle() const
2010-08-30 15:04:46 +08:00
{
return "Logic test";
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// PauseTest
//
//------------------------------------------------------------------
void PauseTest::onEnter()
{
//
// This test MUST be done in 'onEnter' and not on 'init'
// otherwise the paused action will be resumed at 'onEnter' time
//
ActionManagerTest::onEnter();
auto l = Label::createWithTTF("After 5 seconds grossini should move", "fonts/Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition( Vector2(VisibleRect::center().x, VisibleRect::top().y-75) );
//
// Also, this test MUST be done, after [super onEnter]
//
auto grossini = Sprite::create(s_pathGrossini);
addChild(grossini, 0, kTagGrossini);
grossini->setPosition(VisibleRect::center() );
auto action = MoveBy::create(1, Vector2(150,0));
auto director = Director::getInstance();
director->getActionManager()->addAction(action, grossini, true);
schedule( schedule_selector(PauseTest::unpause), 3);
}
2012-06-08 13:55:28 +08:00
void PauseTest::unpause(float dt)
{
unschedule( schedule_selector(PauseTest::unpause) );
auto node = getChildByTag( kTagGrossini );
auto director = Director::getInstance();
director->getActionManager()->resumeTarget(node);
}
2014-02-28 23:02:22 +08:00
std::string PauseTest::subtitle() const
{
return "Pause Test";
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// RemoveTest
//
//------------------------------------------------------------------
void StopActionTest::onEnter()
{
ActionManagerTest::onEnter();
auto l = Label::createWithTTF("Should not crash", "fonts/Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition( Vector2(VisibleRect::center().x, VisibleRect::top().y - 75) );
auto pMove = MoveBy::create(2, Vector2(200, 0));
auto pCallback = CallFunc::create(CC_CALLBACK_0(StopActionTest::stopAction,this));
auto pSequence = Sequence::create(pMove, pCallback, NULL);
pSequence->setTag(kTagSequence);
auto pChild = Sprite::create(s_pathGrossini);
pChild->setPosition( VisibleRect::center() );
addChild(pChild, 1, kTagGrossini);
pChild->runAction(pSequence);
}
void StopActionTest::stopAction()
{
auto sprite = getChildByTag(kTagGrossini);
sprite->stopActionByTag(kTagSequence);
}
std::string StopActionTest::subtitle() const
2010-08-30 15:04:46 +08:00
{
return "Stop Action Test";
}
2010-08-30 15:04:46 +08:00
//------------------------------------------------------------------
//
// ResumeTest
//
//------------------------------------------------------------------
2014-02-28 23:02:22 +08:00
std::string ResumeTest::subtitle() const
2010-08-30 15:04:46 +08:00
{
return "Resume Test";
}
void ResumeTest::onEnter()
{
2010-09-02 15:15:45 +08:00
ActionManagerTest::onEnter();
2010-08-30 15:04:46 +08:00
auto l = Label::createWithTTF("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi.ttf", 16.0f);
addChild(l);
l->setPosition( Vector2(VisibleRect::center().x, VisibleRect::top().y - 75));
2010-08-30 15:04:46 +08:00
auto pGrossini = Sprite::create(s_pathGrossini);
2010-08-30 15:04:46 +08:00
addChild(pGrossini, 0, kTagGrossini);
pGrossini->setPosition(VisibleRect::center());
2010-08-30 15:04:46 +08:00
pGrossini->runAction(ScaleBy::create(2, 2));
2010-08-30 15:04:46 +08:00
auto director = Director::getInstance();
director->getActionManager()->pauseTarget(pGrossini);
pGrossini->runAction(RotateBy::create(2, 360));
2010-08-30 15:04:46 +08:00
this->schedule(schedule_selector(ResumeTest::resumeGrossini), 3.0f);
}
2012-06-08 13:55:28 +08:00
void ResumeTest::resumeGrossini(float time)
{
this->unschedule(schedule_selector(ResumeTest::resumeGrossini));
auto pGrossini = getChildByTag(kTagGrossini);
auto director = Director::getInstance();
director->getActionManager()->resumeTarget(pGrossini);
2010-08-30 15:04:46 +08:00
}
//------------------------------------------------------------------
//
// ActionManagerTestScene
//
//------------------------------------------------------------------
void ActionManagerTestScene::runThisTest()
{
auto layer = nextActionManagerAction();
addChild(layer);
Director::getInstance()->replaceScene(this);
}