PuzzleSDK
ParallaxTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "ParallaxTest.h"
26#include "../testResource.h"
27
29
30enum
31{
34};
35
36ParallaxTests::ParallaxTests()
37{
41}
42
43//------------------------------------------------------------------
44//
45// Parallax1
46//
47//------------------------------------------------------------------
48
50{
51 // Top Layer, a simple image
52 auto cocosImage = Sprite::create(s_Power);
53 // scale the image (optional)
54 cocosImage->setScale( 2.5f );
55 // change the transform anchor point to 0,0 (optional)
56 cocosImage->setAnchorPoint( Vec2(0,0) );
57
58
59 // Middle layer: a Tile map atlas
60 auto tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
61 tilemap->releaseMap();
62
63 // change the transform anchor to 0,0 (optional)
64 tilemap->setAnchorPoint( Vec2(0, 0) );
65
66 // Anti Aliased images
67 tilemap->getTexture()->setAntiAliasTexParameters();
68
69
70 // background layer: another image
71 auto background = Sprite::create(s_back);
72 // scale the image (optional)
73 background->setScale( 1.5f );
74 // change the transform anchor point (optional)
75 background->setAnchorPoint( Vec2(0,0) );
76
77
78 // create a void node, a parent node
79 auto voidNode = ParallaxNode::create();
80
81 // NOW add the 3 layers to the 'void' node
82
83 // background image is moved at a ratio of 0.4x, 0.5y
84 voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
85
86 // tiles are moved at a ratio of 2.2x, 1.0y
87 voidNode->addChild(tilemap, 1, Vec2(2.2f,1.0f), Vec2(0,-200) );
88
89 // top image is moved at a ratio of 3.0x, 2.5y
90 voidNode->addChild(cocosImage, 2, Vec2(3.0f,2.5f), Vec2(200,800) );
91
92
93 // now create some actions that will move the 'void' node
94 // and the children of the 'void' node will move at different
95 // speed, thus, simulation the 3D environment
96 auto goUp = MoveBy::create(4, Vec2(0,-500) );
97 auto goDown = goUp->reverse();
98 auto go = MoveBy::create(8, Vec2(-1000,0) );
99 auto goBack = go->reverse();
100 auto seq = Sequence::create(goUp, go, goDown, goBack, nullptr);
101 voidNode->runAction( (RepeatForever::create(seq) ));
102
103 addChild( voidNode );
104}
105
106std::string Parallax1::title() const
107{
108 return "Parallax: parent and 3 children";
109}
110
111//------------------------------------------------------------------
112//
113// Parallax2
114//
115//------------------------------------------------------------------
116
118{
119 auto listener = EventListenerTouchAllAtOnce::create();
120 listener->onTouchesMoved = CC_CALLBACK_2(Parallax2::onTouchesMoved, this);
121 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
122
123 // Top Layer, a simple image
124 auto cocosImage = Sprite::create(s_Power);
125 // scale the image (optional)
126 cocosImage->setScale( 2.5f );
127 // change the transform anchor point to 0,0 (optional)
128 cocosImage->setAnchorPoint( Vec2(0,0) );
129
130
131 // Middle layer: a Tile map atlas
132 auto tilemap = TileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16);
133 tilemap->releaseMap();
134
135 // change the transform anchor to 0,0 (optional)
136 tilemap->setAnchorPoint( Vec2(0, 0) );
137
138 // Anti Aliased images
139 tilemap->getTexture()->setAntiAliasTexParameters();
140
141
142 // background layer: another image
143 auto background = Sprite::create(s_back);
144 // scale the image (optional)
145 background->setScale( 1.5f );
146 // change the transform anchor point (optional)
147 background->setAnchorPoint( Vec2(0,0) );
148
149
150 // create a void node, a parent node
151 auto voidNode = ParallaxNode::create();
152
153 // NOW add the 3 layers to the 'void' node
154
155 // background image is moved at a ratio of 0.4x, 0.5y
156 voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
157
158 // tiles are moved at a ratio of 1.0, 1.0y
159 voidNode->addChild(tilemap, 1, Vec2(1.0f,1.0f), Vec2(0,-200) );
160
161 // top image is moved at a ratio of 3.0x, 2.5y
162 voidNode->addChild( cocosImage, 2, Vec2(3.0f,2.5f), Vec2(200,1000) );
163 addChild(voidNode, 0, kTagNode);
164}
165
166void Parallax2::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
167{
168 auto diff = touches[0]->getDelta();
169
170 auto node = getChildByTag(kTagNode);
171 auto currentPos = node->getPosition();
172 node->setPosition(currentPos + diff);
173}
174
175std::string Parallax2::title() const
176{
177 return "Parallax: drag screen";
178}
179
180//------------------------------------------------------------------
181//
182// Issue2572
183//
184//------------------------------------------------------------------
186: _moveTimer(0.0f)
187, _addTimer(0.0f)
188, _preListSize(0)
189, _printCount(0)
190{
191 _addChildStep = 1.0f;
192 _wholeMoveTime = 3.0f;
193 _wholeMoveSize = Vec2(-300, 0);
194
195 // create a parallax node, a parent node
196 _paraNode = ParallaxNode::create();
197 addChild(_paraNode, 0, kTagNode);
198
199 this->scheduleUpdate();
200}
201
202void Issue2572::update(float dt)
203{
204 _addTimer += dt;
205 _moveTimer += dt;
206 if (_moveTimer >= _wholeMoveTime) {
207 this->unscheduleUpdate();
208 return;
209 }
210
211 _paraNode->setPosition(_paraNode->getPosition() + _wholeMoveSize * dt / _wholeMoveTime);
212
213 if (_addTimer >= _addChildStep) {
214 _addTimer = 0.0f;
215
216 auto child = Sprite::create("Images/Icon.png");
217 Size viewSize = Director::getInstance()->getVisibleSize();
218 Vec2 offset = Vec2(viewSize.width / 2, viewSize.height/2);
219 _paraNode->addChild(child, 1, Vec2( 1, 0 ), offset );
220
221 _childList.pushBack(child);
222 }
223
224 // After a child added, output the position of the children 3 times.
225 // Bug : The first output is much different with the second one & the third one.
226 if (_childList.size() != _preListSize) {
227 switch (_printCount) {
228 case 0:
229 case 1:
230 case 2:
231 log( "--child count-- %zd", _childList.size());
232 for (const auto& obj : _childList)
233 {
234 Sprite* obstacle = dynamic_cast<Sprite*>( obj );
235 log("child position : (%.2f, %.2f)", obstacle->getPositionX(), obstacle->getPositionY());
236 }
237 log("-------------------");
238 _printCount++;
239 break;
240 case 3:
241 _preListSize = _childList.size();
242 _printCount = 0;
243 break;
244 default:
245 break;
246 }
247 }
248}
249
250std::string Issue2572::title() const
251{
252 return "Issue 2572";
253}
254
255std::string Issue2572::subtitle() const
256{
257 return "Look at the output in console";
258}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
@ kTagGrossini
@ kTagNode
USING_NS_CC
cocos2d::Vector< cocos2d::Sprite * > _childList
Definition: ParallaxTest.h:73
virtual std::string subtitle() const override
virtual std::string title() const override
cocos2d::Vec2 _wholeMoveSize
Definition: ParallaxTest.h:79
float _moveTimer
Definition: ParallaxTest.h:71
float _addChildStep
Definition: ParallaxTest.h:77
cocos2d::ParallaxNode * _paraNode
Definition: ParallaxTest.h:70
float _wholeMoveTime
Definition: ParallaxTest.h:78
ssize_t _preListSize
Definition: ParallaxTest.h:74
int _printCount
Definition: ParallaxTest.h:75
virtual void update(float dt) override
float _addTimer
Definition: ParallaxTest.h:72
virtual std::string title() const override
virtual std::string title() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
static const char s_TilesPng[]
Definition: testResource.h:63
static const char s_LevelMapTga[]
Definition: testResource.h:64
static const char s_back[]
Definition: testResource.h:38
static const char s_Power[]
Definition: testResource.h:59