axmol/samples/Cpp/TestCpp/Classes/MotionStreakTest/MotionStreakTest.cpp

283 lines
6.5 KiB
C++
Raw Normal View History

#include "MotionStreakTest.h"
#include "../testResource.h"
2012-06-12 16:56:34 +08:00
enum {
kTagLabel = 1,
kTagSprite1 = 2,
kTagSprite2 = 3,
};
Layer* nextMotionAction();
Layer* backMotionAction();
Layer* restartMotionAction();
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest1
//
//------------------------------------------------------------------
2010-08-25 17:02:58 +08:00
void MotionStreakTest1::onEnter()
{
MotionStreakTest::onEnter();
2010-08-25 17:02:58 +08:00
Size s = Director::getInstance()->getWinSize();
2010-08-25 17:02:58 +08:00
// the root object just rotates around
_root = Sprite::create(s_pPathR1);
addChild(_root, 1);
2013-07-12 14:11:55 +08:00
_root->setPosition(Point(s.width/2, s.height/2));
2010-08-25 17:02:58 +08:00
// the target object is offset from root, and the streak is moved to follow it
_target = Sprite::create(s_pPathR1);
_root->addChild(_target);
2013-07-12 14:11:55 +08:00
_target->setPosition(Point(s.width/4, 0));
// create the streak object and add it to the scene
streak = MotionStreak::create(2, 3, 32, Color3B::GREEN, s_streak);
2012-06-12 16:56:34 +08:00
addChild(streak);
// schedule an update on each frame so we can syncronize the streak with the target
schedule(schedule_selector(MotionStreakTest1::onUpdate));
2010-08-25 17:02:58 +08:00
ActionInterval* a1 = RotateBy::create(2, 360);
2010-08-25 17:02:58 +08:00
Action* action1 = RepeatForever::create(a1);
2013-07-12 14:11:55 +08:00
ActionInterval* motion = MoveBy::create(2, Point(100,0) );
_root->runAction( RepeatForever::create(Sequence::create(motion, motion->reverse(), NULL) ) );
_root->runAction( action1 );
2012-03-22 14:32:32 +08:00
ActionInterval *colorAction = RepeatForever::create(Sequence::create(
TintTo::create(0.2f, 255, 0, 0),
TintTo::create(0.2f, 0, 255, 0),
TintTo::create(0.2f, 0, 0, 255),
TintTo::create(0.2f, 0, 255, 255),
TintTo::create(0.2f, 255, 255, 0),
TintTo::create(0.2f, 255, 0, 255),
TintTo::create(0.2f, 255, 255, 255),
NULL));
2012-03-22 14:32:32 +08:00
2012-06-12 16:56:34 +08:00
streak->runAction(colorAction);
2010-08-25 17:02:58 +08:00
}
2012-06-08 13:55:28 +08:00
void MotionStreakTest1::onUpdate(float delta)
{
streak->setPosition( _target->convertToWorldSpace(Point::ZERO) );
}
2010-08-25 17:02:58 +08:00
std::string MotionStreakTest1::title()
{
return "MotionStreak test 1";
}
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest2
//
//------------------------------------------------------------------
2010-08-25 17:02:58 +08:00
void MotionStreakTest2::onEnter()
{
MotionStreakTest::onEnter();
2010-08-25 17:02:58 +08:00
setTouchEnabled(true);
2010-08-25 17:02:58 +08:00
Size s = Director::getInstance()->getWinSize();
// create the streak object and add it to the scene
streak = MotionStreak::create(3, 3, 64, Color3B::WHITE, s_streak );
2012-06-12 16:56:34 +08:00
addChild(streak);
2013-07-12 14:11:55 +08:00
streak->setPosition( Point(s.width/2, s.height/2) );
2010-08-25 17:02:58 +08:00
}
void MotionStreakTest2::ccTouchesMoved(Set* touches, Event* event)
2010-08-25 17:02:58 +08:00
{
Touch* touch = static_cast<Touch*>( touches->anyObject() );
2010-08-25 17:02:58 +08:00
Point touchLocation = touch->getLocation();
2012-06-12 16:56:34 +08:00
streak->setPosition( touchLocation );
}
2010-08-25 17:02:58 +08:00
std::string MotionStreakTest2::title()
{
return "MotionStreak test";
}
2012-06-12 16:56:34 +08:00
//------------------------------------------------------------------
//
// Issue1358
//
//------------------------------------------------------------------
void Issue1358::onEnter()
{
MotionStreakTest::onEnter();
// ask director the the window size
Size size = Director::getInstance()->getWinSize();
2012-06-12 16:56:34 +08:00
streak = MotionStreak::create(2.0f, 1.0f, 50.0f, Color3B(255, 255, 0), "Images/Icon.png");
2012-06-12 16:56:34 +08:00
addChild(streak);
2013-07-12 14:11:55 +08:00
_center = Point(size.width/2, size.height/2);
_radius = size.width/3;
_angle = 0.0f;
2012-06-12 16:56:34 +08:00
schedule(schedule_selector(Issue1358::update), 0);
}
void Issue1358::update(float dt)
{
_angle += 1.0f;
2013-07-12 14:11:55 +08:00
streak->setPosition(Point(_center.x + cosf(_angle/180 * M_PI)*_radius,
_center.y + sinf(_angle/ 180 * M_PI)*_radius));
2012-06-12 16:56:34 +08:00
}
std::string Issue1358::title()
{
return "Issue 1358";
}
std::string Issue1358::subtitle()
{
return "The tail should use the texture";
}
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest
//
//------------------------------------------------------------------
// enum
// {
// IDC_NEXT = 100,
// IDC_BACK,
// IDC_RESTART
// };
static int sceneIdx = -1;
2012-06-12 16:56:34 +08:00
#define MAX_LAYER 3
2010-08-25 17:02:58 +08:00
Layer* createMotionLayer(int nIndex)
2010-08-25 17:02:58 +08:00
{
switch(nIndex)
{
case 0: return new MotionStreakTest1();
case 1: return new MotionStreakTest2();
2012-06-12 16:56:34 +08:00
case 2: return new Issue1358();
}
2010-08-25 17:02:58 +08:00
return NULL;
2010-08-25 17:02:58 +08:00
}
Layer* nextMotionAction()
2010-08-25 17:02:58 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
2010-08-25 17:02:58 +08:00
Layer* pLayer = createMotionLayer(sceneIdx);
pLayer->autorelease();
2010-08-25 17:02:58 +08:00
return pLayer;
2010-08-25 17:02:58 +08:00
}
Layer* backMotionAction()
2010-08-25 17:02:58 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
Layer* pLayer = createMotionLayer(sceneIdx);
pLayer->autorelease();
2010-08-25 17:02:58 +08:00
return pLayer;
2010-08-25 17:02:58 +08:00
}
Layer* restartMotionAction()
2010-08-25 17:02:58 +08:00
{
Layer* pLayer = createMotionLayer(sceneIdx);
pLayer->autorelease();
2010-08-25 17:02:58 +08:00
return pLayer;
}
MotionStreakTest::MotionStreakTest(void)
{
}
MotionStreakTest::~MotionStreakTest(void)
{
}
2010-08-25 17:02:58 +08:00
std::string MotionStreakTest::title()
{
return "No title";
2010-08-25 17:02:58 +08:00
}
2012-06-12 16:56:34 +08:00
std::string MotionStreakTest::subtitle()
{
return "";
}
2010-08-25 17:02:58 +08:00
void MotionStreakTest::onEnter()
{
BaseTest::onEnter();
2010-08-25 17:02:58 +08:00
Size s = Director::getInstance()->getWinSize();
2010-08-25 17:02:58 +08:00
MenuItemToggle *itemMode = MenuItemToggle::createWithCallback( CC_CALLBACK_1(MotionStreakTest::modeCallback, this),
MenuItemFont::create("Use High Quality Mode"),
MenuItemFont::create("Use Fast Mode"),
NULL);
2012-03-22 14:32:32 +08:00
Menu *menuMode = Menu::create(itemMode, NULL);
addChild(menuMode);
2012-03-22 14:32:32 +08:00
2013-07-12 14:11:55 +08:00
menuMode->setPosition(Point(s.width/2, s.height/4));
2012-03-22 14:32:32 +08:00
}
void MotionStreakTest::modeCallback(Object *pSender)
2012-03-22 14:32:32 +08:00
{
bool fastMode = streak->isFastMode();
streak->setFastMode(! fastMode);
2010-08-25 17:02:58 +08:00
}
void MotionStreakTest::restartCallback(Object* pSender)
2010-08-25 17:02:58 +08:00
{
Scene* s = new MotionStreakTestScene();//CCScene::create();
s->addChild(restartMotionAction());
2010-08-25 17:02:58 +08:00
Director::getInstance()->replaceScene(s);
s->release();
2010-08-25 17:02:58 +08:00
}
void MotionStreakTest::nextCallback(Object* pSender)
2010-08-25 17:02:58 +08:00
{
Scene* s = new MotionStreakTestScene();//CCScene::create();
s->addChild( nextMotionAction() );
Director::getInstance()->replaceScene(s);
s->release();
2010-08-25 17:02:58 +08:00
}
void MotionStreakTest::backCallback(Object* pSender)
2010-08-25 17:02:58 +08:00
{
Scene* s = new MotionStreakTestScene;//CCScene::create();
s->addChild( backMotionAction() );
Director::getInstance()->replaceScene(s);
s->release();
2010-08-25 17:02:58 +08:00
}
void MotionStreakTestScene::runThisTest()
{
Layer* pLayer = nextMotionAction();
2010-08-25 17:02:58 +08:00
addChild(pLayer);
Director::getInstance()->replaceScene(this);
2010-08-25 17:02:58 +08:00
}