2012-04-19 14:35:52 +08:00
|
|
|
#include "ParallaxTest.h"
|
|
|
|
#include "../testResource.h"
|
2010-09-01 14:57:55 +08:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kTagNode,
|
|
|
|
kTagGrossini,
|
2010-09-01 14:57:55 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* nextParallaxAction();
|
|
|
|
Layer* backParallaxAction();
|
|
|
|
Layer* restartParallaxAction();
|
2011-07-08 15:57:46 +08:00
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Parallax1
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
Parallax1::Parallax1()
|
|
|
|
{
|
|
|
|
// Top Layer, a simple image
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite* cocosImage = Sprite::create(s_Power);
|
2012-04-19 14:35:52 +08:00
|
|
|
// scale the image (optional)
|
|
|
|
cocosImage->setScale( 2.5f );
|
|
|
|
// change the transform anchor point to 0,0 (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
cocosImage->setAnchorPoint( Point(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Middle layer: a Tile map atlas
|
2013-06-20 14:17:10 +08:00
|
|
|
TileMapAtlas *tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
|
2012-04-19 14:35:52 +08:00
|
|
|
tilemap->releaseMap();
|
|
|
|
|
|
|
|
// change the transform anchor to 0,0 (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
tilemap->setAnchorPoint( Point(0, 0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Anti Aliased images
|
2011-02-23 16:47:25 +08:00
|
|
|
tilemap->getTexture()->setAntiAliasTexParameters();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// background layer: another image
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite* background = Sprite::create(s_back);
|
2012-04-19 14:35:52 +08:00
|
|
|
// scale the image (optional)
|
|
|
|
background->setScale( 1.5f );
|
|
|
|
// change the transform anchor point (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
background->setAnchorPoint( Point(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// create a void node, a parent node
|
2013-06-20 14:17:10 +08:00
|
|
|
ParallaxNode* voidNode = ParallaxNode::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// NOW add the 3 layers to the 'void' node
|
|
|
|
|
|
|
|
// background image is moved at a ratio of 0.4x, 0.5y
|
2013-07-12 14:47:36 +08:00
|
|
|
voidNode->addChild(background, -1, Point(0.4f,0.5f), Point::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tiles are moved at a ratio of 2.2x, 1.0y
|
2013-07-12 14:11:55 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Point(2.2f,1.0f), Point(0,-200) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2013-07-12 14:11:55 +08:00
|
|
|
voidNode->addChild(cocosImage, 2, Point(3.0f,2.5f), Point(200,800) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// now create some actions that will move the 'void' node
|
|
|
|
// and the children of the 'void' node will move at different
|
|
|
|
// speed, thus, simulation the 3D environment
|
2013-07-12 14:11:55 +08:00
|
|
|
ActionInterval* goUp = MoveBy::create(4, Point(0,-500) );
|
2013-06-20 14:17:10 +08:00
|
|
|
ActionInterval* goDown = goUp->reverse();
|
2013-07-12 14:11:55 +08:00
|
|
|
ActionInterval* go = MoveBy::create(8, Point(-1000,0) );
|
2013-06-20 14:17:10 +08:00
|
|
|
ActionInterval* goBack = go->reverse();
|
|
|
|
Sequence* seq = Sequence::create(goUp, go, goDown, goBack, NULL);
|
|
|
|
voidNode->runAction( (RepeatForever::create(seq) ));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
addChild( voidNode );
|
|
|
|
}
|
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
std::string Parallax1::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Parallax: parent and 3 children";
|
|
|
|
}
|
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Parallax2
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
Parallax2::Parallax2()
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled( true );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Top Layer, a simple image
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite* cocosImage = Sprite::create(s_Power);
|
2012-04-19 14:35:52 +08:00
|
|
|
// scale the image (optional)
|
|
|
|
cocosImage->setScale( 2.5f );
|
|
|
|
// change the transform anchor point to 0,0 (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
cocosImage->setAnchorPoint( Point(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Middle layer: a Tile map atlas
|
2013-06-20 14:17:10 +08:00
|
|
|
TileMapAtlas* tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
|
2012-04-19 14:35:52 +08:00
|
|
|
tilemap->releaseMap();
|
|
|
|
|
|
|
|
// change the transform anchor to 0,0 (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
tilemap->setAnchorPoint( Point(0, 0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-02-23 16:47:25 +08:00
|
|
|
// Anti Aliased images
|
|
|
|
tilemap->getTexture()->setAntiAliasTexParameters();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// background layer: another image
|
2013-06-20 14:17:10 +08:00
|
|
|
Sprite* background = Sprite::create(s_back);
|
2012-04-19 14:35:52 +08:00
|
|
|
// scale the image (optional)
|
|
|
|
background->setScale( 1.5f );
|
|
|
|
// change the transform anchor point (optional)
|
2013-07-12 14:11:55 +08:00
|
|
|
background->setAnchorPoint( Point(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// create a void node, a parent node
|
2013-06-20 14:17:10 +08:00
|
|
|
ParallaxNode* voidNode = ParallaxNode::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// NOW add the 3 layers to the 'void' node
|
|
|
|
|
|
|
|
// background image is moved at a ratio of 0.4x, 0.5y
|
2013-07-12 14:47:36 +08:00
|
|
|
voidNode->addChild(background, -1, Point(0.4f,0.5f), Point::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tiles are moved at a ratio of 1.0, 1.0y
|
2013-07-12 14:11:55 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Point(1.0f,1.0f), Point(0,-200) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2013-07-12 14:11:55 +08:00
|
|
|
voidNode->addChild( cocosImage, 2, Point(3.0f,2.5f), Point(200,1000) );
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(voidNode, 0, kTagNode);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void Parallax2::ccTouchesMoved(Set *pTouches, Event *pEvent)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Touch *touch = (Touch*)pTouches->anyObject();
|
|
|
|
Point diff = touch->getDelta();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Node* node = getChildByTag(kTagNode);
|
|
|
|
Point currentPos = node->getPosition();
|
2013-07-11 16:38:58 +08:00
|
|
|
node->setPosition(currentPos + diff);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
std::string Parallax2::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Parallax: drag screen";
|
|
|
|
}
|
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// ParallaxDemo
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
static int sceneIdx = -1;
|
|
|
|
|
|
|
|
#define MAX_LAYER 2
|
2010-09-01 14:57:55 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* createParallaxTestLayer(int nIndex)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
switch(nIndex)
|
|
|
|
{
|
|
|
|
case 0: return new Parallax1();
|
|
|
|
case 1: return new Parallax2();
|
|
|
|
}
|
2010-09-01 14:57:55 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* nextParallaxAction()
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
2010-09-01 14:57:55 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = createParallaxTestLayer(sceneIdx);
|
2012-04-19 14:35:52 +08:00
|
|
|
pLayer->autorelease();
|
2010-09-01 14:57:55 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pLayer;
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* backParallaxAction()
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = createParallaxTestLayer(sceneIdx);
|
2012-04-19 14:35:52 +08:00
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* restartParallaxAction()
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = createParallaxTestLayer(sceneIdx);
|
2012-04-19 14:35:52 +08:00
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ParallaxDemo::ParallaxDemo(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ParallaxDemo::~ParallaxDemo(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-01 14:57:55 +08:00
|
|
|
std::string ParallaxDemo::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "No title";
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxDemo::onEnter()
|
|
|
|
{
|
2013-06-07 08:12:28 +08:00
|
|
|
BaseTest::onEnter();
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ParallaxDemo::restartCallback(Object* pSender)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new ParallaxTestScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild(restartParallaxAction());
|
2010-09-01 14:57:55 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2010-09-13 18:04:36 +08:00
|
|
|
s->release();
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ParallaxDemo::nextCallback(Object* pSender)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new ParallaxTestScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( nextParallaxAction() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2010-09-13 18:04:36 +08:00
|
|
|
s->release();
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ParallaxDemo::backCallback(Object* pSender)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new ParallaxTestScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( backParallaxAction() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2010-09-13 18:04:36 +08:00
|
|
|
s->release();
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxTestScene::runThisTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = nextParallaxAction();
|
2010-09-01 14:57:55 +08:00
|
|
|
|
|
|
|
addChild(pLayer);
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2010-09-01 14:57:55 +08:00
|
|
|
}
|