2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ParallaxTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
enum
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
kTagNode,
|
|
|
|
kTagGrossini,
|
|
|
|
};
|
|
|
|
|
|
|
|
ParallaxTests::ParallaxTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(Parallax1);
|
|
|
|
ADD_TEST_CASE(Parallax2);
|
|
|
|
ADD_TEST_CASE(Issue2572);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Parallax1
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
Parallax1::Parallax1()
|
|
|
|
{
|
|
|
|
// Top Layer, a simple image
|
|
|
|
auto cocosImage = Sprite::create(s_Power);
|
|
|
|
// scale the image (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
cocosImage->setScale(2.5f);
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor point to 0,0 (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
cocosImage->setAnchorPoint(Vec2(0, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Middle layer: a Tile map atlas
|
|
|
|
auto tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
|
|
|
|
tilemap->releaseMap();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor to 0,0 (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
tilemap->setAnchorPoint(Vec2(0, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Anti Aliased images
|
|
|
|
tilemap->getTexture()->setAntiAliasTexParameters();
|
|
|
|
|
|
|
|
// background layer: another image
|
|
|
|
auto background = Sprite::create(s_back);
|
|
|
|
// scale the image (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
background->setScale(1.5f);
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor point (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
background->setAnchorPoint(Vec2(0, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// create a void node, a parent node
|
|
|
|
auto voidNode = ParallaxNode::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// NOW add the 3 layers to the 'void' node
|
|
|
|
|
|
|
|
// background image is moved at a ratio of 0.4x, 0.5y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(background, -1, Vec2(0.4f, 0.5f), Vec2::ZERO);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// tiles are moved at a ratio of 2.2x, 1.0y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Vec2(2.2f, 1.0f), Vec2(0, -200));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(cocosImage, 2, Vec2(3.0f, 2.5f), Vec2(200, 800));
|
|
|
|
|
2019-11-23 20:27:39 +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
|
2021-12-28 21:27:32 +08:00
|
|
|
auto goUp = MoveBy::create(4, Vec2(0, -500));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto goDown = goUp->reverse();
|
2021-12-28 21:27:32 +08:00
|
|
|
auto go = MoveBy::create(8, Vec2(-1000, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
auto goBack = go->reverse();
|
2021-12-28 21:27:32 +08:00
|
|
|
auto seq = Sequence::create(goUp, go, goDown, goBack, nullptr);
|
|
|
|
voidNode->runAction((RepeatForever::create(seq)));
|
|
|
|
|
|
|
|
addChild(voidNode);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Parallax1::title() const
|
|
|
|
{
|
|
|
|
return "Parallax: parent and 3 children";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Parallax2
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
Parallax2::Parallax2()
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Parallax2::onTouchesMoved, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Top Layer, a simple image
|
|
|
|
auto cocosImage = Sprite::create(s_Power);
|
|
|
|
// scale the image (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
cocosImage->setScale(2.5f);
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor point to 0,0 (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
cocosImage->setAnchorPoint(Vec2(0, 0));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Middle layer: a Tile map atlas
|
|
|
|
auto tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
|
|
|
|
tilemap->releaseMap();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor to 0,0 (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
tilemap->setAnchorPoint(Vec2(0, 0));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Anti Aliased images
|
|
|
|
tilemap->getTexture()->setAntiAliasTexParameters();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// background layer: another image
|
|
|
|
auto background = Sprite::create(s_back);
|
|
|
|
// scale the image (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
background->setScale(1.5f);
|
2019-11-23 20:27:39 +08:00
|
|
|
// change the transform anchor point (optional)
|
2021-12-28 21:27:32 +08:00
|
|
|
background->setAnchorPoint(Vec2(0, 0));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// create a void node, a parent node
|
|
|
|
auto voidNode = ParallaxNode::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// NOW add the 3 layers to the 'void' node
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// background image is moved at a ratio of 0.4x, 0.5y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(background, -1, Vec2(0.4f, 0.5f), Vec2::ZERO);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// tiles are moved at a ratio of 1.0, 1.0y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(tilemap, 1, Vec2(1.0f, 1.0f), Vec2(0, -200));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// top image is moved at a ratio of 3.0x, 2.5y
|
2021-12-28 21:27:32 +08:00
|
|
|
voidNode->addChild(cocosImage, 2, Vec2(3.0f, 2.5f), Vec2(200, 1000));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(voidNode, 0, kTagNode);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void Parallax2::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto diff = touches[0]->getDelta();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
auto node = getChildByTag(kTagNode);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto currentPos = node->getPosition();
|
|
|
|
node->setPosition(currentPos + diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Parallax2::title() const
|
|
|
|
{
|
|
|
|
return "Parallax: drag screen";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Issue2572
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2021-12-28 21:27:32 +08:00
|
|
|
Issue2572::Issue2572() : _moveTimer(0.0f), _addTimer(0.0f), _preListSize(0), _printCount(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
_addChildStep = 1.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
_wholeMoveTime = 3.0f;
|
|
|
|
_wholeMoveSize = Vec2(-300, 0);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +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;
|
2021-12-28 21:27:32 +08:00
|
|
|
if (_moveTimer >= _wholeMoveTime)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->unscheduleUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_paraNode->setPosition(_paraNode->getPosition() + _wholeMoveSize * dt / _wholeMoveTime);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
if (_addTimer >= _addChildStep)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_addTimer = 0.0f;
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
auto child = Sprite::create("Images/Icon.png");
|
2019-11-23 20:27:39 +08:00
|
|
|
Size viewSize = Director::getInstance()->getVisibleSize();
|
2021-12-28 21:27:32 +08:00
|
|
|
Vec2 offset = Vec2(viewSize.width / 2, viewSize.height / 2);
|
|
|
|
_paraNode->addChild(child, 1, Vec2(1, 0), offset);
|
|
|
|
|
2019-11-23 20:27:39 +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.
|
2021-12-28 21:27:32 +08:00
|
|
|
if (_childList.size() != _preListSize)
|
|
|
|
{
|
|
|
|
switch (_printCount)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
log("--child count-- %zd", _childList.size());
|
|
|
|
for (const auto& obj : _childList)
|
|
|
|
{
|
|
|
|
Sprite* obstacle = dynamic_cast<Sprite*>(obj);
|
|
|
|
log("child position : (%.2f, %.2f)", obstacle->getPositionX(), obstacle->getPositionY());
|
|
|
|
}
|
|
|
|
log("-------------------");
|
|
|
|
_printCount++;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
_preListSize = _childList.size();
|
|
|
|
_printCount = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue2572::title() const
|
|
|
|
{
|
|
|
|
return "Issue 2572";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue2572::subtitle() const
|
|
|
|
{
|
|
|
|
return "Look at the output in console";
|
|
|
|
}
|