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
|
|
|
};
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
ParallaxTests::ParallaxTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(Parallax1);
|
|
|
|
ADD_TEST_CASE(Parallax2);
|
|
|
|
ADD_TEST_CASE(Issue2572);
|
|
|
|
}
|
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-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
cocosImage->setAnchorPoint( Vec2(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Middle layer: a Tile map atlas
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
tilemap->setAnchorPoint( Vec2(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-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
background->setAnchorPoint( Vec2(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// create a void node, a parent node
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tiles are moved at a ratio of 2.2x, 1.0y
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Vec2(2.2f,1.0f), Vec2(0,-200) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild(cocosImage, 2, Vec2(3.0f,2.5f), Vec2(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
|
2014-05-15 01:07:09 +08:00
|
|
|
auto goUp = MoveBy::create(4, Vec2(0,-500) );
|
2013-08-16 16:05:27 +08:00
|
|
|
auto goDown = goUp->reverse();
|
2014-05-15 01:07:09 +08:00
|
|
|
auto go = MoveBy::create(8, Vec2(-1000,0) );
|
2013-08-16 16:05:27 +08:00
|
|
|
auto goBack = go->reverse();
|
2014-07-10 00:45:27 +08:00
|
|
|
auto seq = Sequence::create(goUp, go, goDown, goBack, nullptr);
|
2013-06-20 14:17:10 +08:00
|
|
|
voidNode->runAction( (RepeatForever::create(seq) ));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
addChild( voidNode );
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string Parallax1::title() const
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
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()
|
|
|
|
{
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Parallax2::onTouchesMoved, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Top Layer, a simple image
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
cocosImage->setAnchorPoint( Vec2(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Middle layer: a Tile map atlas
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
tilemap->setAnchorPoint( Vec2(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-08-16 16:05:27 +08:00
|
|
|
auto 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)
|
2014-05-15 01:07:09 +08:00
|
|
|
background->setAnchorPoint( Vec2(0,0) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// create a void node, a parent node
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tiles are moved at a ratio of 1.0, 1.0y
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Vec2(1.0f,1.0f), Vec2(0,-200) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2014-05-15 01:07:09 +08:00
|
|
|
voidNode->addChild( cocosImage, 2, Vec2(3.0f,2.5f), Vec2(200,1000) );
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(voidNode, 0, kTagNode);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Parallax2::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
auto diff = touches[0]->getDelta();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto node = getChildByTag(kTagNode);
|
|
|
|
auto currentPos = node->getPosition();
|
2013-07-11 16:38:58 +08:00
|
|
|
node->setPosition(currentPos + diff);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string Parallax2::title() const
|
2010-09-01 14:57:55 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return "Parallax: drag screen";
|
|
|
|
}
|
|
|
|
|
2014-01-22 15:38:59 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Issue2572
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
Issue2572::Issue2572()
|
2015-03-13 06:47:58 +08:00
|
|
|
: _moveTimer(0.0f)
|
2014-01-22 15:38:59 +08:00
|
|
|
, _addTimer(0.0f)
|
2015-03-13 06:47:58 +08:00
|
|
|
, _preListSize(0)
|
|
|
|
, _printCount(0)
|
2014-01-22 15:38:59 +08:00
|
|
|
{
|
2014-01-22 17:04:54 +08:00
|
|
|
_addChildStep = 1.0f;
|
|
|
|
_wholeMoveTime = 3.0f;
|
2014-05-15 01:07:09 +08:00
|
|
|
_wholeMoveSize = Vec2(-300, 0);
|
2014-01-22 17:04:54 +08:00
|
|
|
|
2014-01-22 15:38:59 +08:00
|
|
|
// create a parallax node, a parent node
|
|
|
|
_paraNode = ParallaxNode::create();
|
|
|
|
addChild(_paraNode, 0, kTagNode);
|
|
|
|
|
|
|
|
this->scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Issue2572::update(float dt)
|
|
|
|
{
|
|
|
|
_addTimer += dt;
|
|
|
|
_moveTimer += dt;
|
|
|
|
if (_moveTimer >= _wholeMoveTime) {
|
|
|
|
this->unscheduleUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_paraNode->setPosition(_paraNode->getPosition() + _wholeMoveSize * dt / _wholeMoveTime);
|
|
|
|
|
|
|
|
if (_addTimer >= _addChildStep) {
|
|
|
|
_addTimer = 0.0f;
|
|
|
|
|
|
|
|
auto child = Sprite::create("Images/Icon.png");
|
|
|
|
Size viewSize = Director::getInstance()->getVisibleSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 offset = Vec2(viewSize.width / 2, viewSize.height/2);
|
|
|
|
_paraNode->addChild(child, 1, Vec2( 1, 0 ), offset );
|
2014-01-22 15:38:59 +08:00
|
|
|
|
|
|
|
_childList.pushBack(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
// After a child added, output the position of the children 3 times.
|
|
|
|
// Bug : The first output is much different with the second one & the third one.
|
|
|
|
if (_childList.size() != _preListSize) {
|
|
|
|
switch (_printCount) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
log( "--child count-- %zd", _childList.size());
|
2014-01-22 17:04:54 +08:00
|
|
|
for (const auto& obj : _childList)
|
2014-01-22 15:38:59 +08:00
|
|
|
{
|
|
|
|
Sprite* obstacle = dynamic_cast<Sprite*>( obj );
|
2014-01-22 17:04:54 +08:00
|
|
|
log("child position : (%.2f, %.2f)", obstacle->getPositionX(), obstacle->getPositionY());
|
2014-01-22 15:38:59 +08:00
|
|
|
}
|
|
|
|
log("-------------------");
|
|
|
|
_printCount++;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
_preListSize = _childList.size();
|
|
|
|
_printCount = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue2572::title() const
|
|
|
|
{
|
|
|
|
return "Issue 2572";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue2572::subtitle() const
|
|
|
|
{
|
|
|
|
return "Look at the output in console";
|
|
|
|
}
|