PuzzleSDK
ActionManagerTest.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 "ActionManagerTest.h"
26#include "../testResource.h"
27#include "cocos2d.h"
28
30
31enum
32{
36};
37
38ActionManagerTests::ActionManagerTests()
39{
48}
49
50//------------------------------------------------------------------
51//
52// ActionManagerTest
53//
54//------------------------------------------------------------------
55
57{
58}
59
61{
62}
63
64std::string ActionManagerTest::title() const
65{
66 return "ActionManager Test";
67}
68std::string ActionManagerTest::subtitle() const
69{
70 return "No title";
71}
72
73//------------------------------------------------------------------
74//
75// Test1
76//
77//------------------------------------------------------------------
78
80{
82
83 auto child = Sprite::create(s_pathGrossini);
84 child->setPosition( VisibleRect::center() );
85 addChild(child, 1, kTagGrossini);
86
87 //Sum of all action's duration is 1.5 second.
88 child->runAction(RotateBy::create(1.5f, 90));
89 child->runAction(Sequence::create(
90 DelayTime::create(1.4f),
91 FadeOut::create(1.1f),
92 nullptr)
93 );
94
95 //After 1.5 second, self will be removed.
96 child->runAction(Sequence::create(
97 DelayTime::create(1.4f),
98 CallFunc::create( CC_CALLBACK_0(CrashTest::removeThis,this)),
99 nullptr)
100 );
101}
102
104{
105 auto child = getChildByTag(kTagGrossini);
106 child->removeChild(child, true);
107
109}
110
111std::string CrashTest::subtitle() const
112{
113 return "Test 1. Should not crash";
114}
115
116//------------------------------------------------------------------
117//
118// Test2
119//
120//------------------------------------------------------------------
122{
124
125 auto grossini = Sprite::create(s_pathGrossini);
126 addChild(grossini, 0, 2);
127 grossini->setPosition(VisibleRect::center());
128
129 grossini->runAction( Sequence::create(
130 MoveBy::create(1, Vec2(150.0f,0.0f)),
131 CallFuncN::create(CC_CALLBACK_1(LogicTest::bugMe,this)),
132 nullptr)
133 );
134}
135
136void LogicTest::bugMe(Node* node)
137{
138 node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
139 node->runAction(ScaleTo::create(2, 2));
140}
141
142std::string LogicTest::subtitle() const
143{
144 return "Logic test";
145}
146
147//------------------------------------------------------------------
148//
149// PauseTest
150//
151//------------------------------------------------------------------
152
154{
155 //
156 // This test MUST be done in 'onEnter' and not on 'init'
157 // otherwise the paused action will be resumed at 'onEnter' time
158 //
160
161
162 auto l = Label::createWithTTF("After 5 seconds grossini should move", "fonts/Thonburi.ttf", 16.0f);
163 addChild(l);
164 l->setPosition(VisibleRect::center().x, VisibleRect::top().y-75);
165
166
167 //
168 // Also, this test MUST be done, after [super onEnter]
169 //
170 auto grossini = Sprite::create(s_pathGrossini);
171 addChild(grossini, 0, kTagGrossini);
172 grossini->setPosition(VisibleRect::center() );
173
174 auto action = MoveBy::create(1, Vec2(150.0f,0.0f));
175
176 auto director = Director::getInstance();
177 director->getActionManager()->addAction(action, grossini, true);
178
179 schedule( CC_SCHEDULE_SELECTOR(PauseTest::unpause), 3);
180}
181
182void PauseTest::unpause(float dt)
183{
184 unschedule( CC_SCHEDULE_SELECTOR(PauseTest::unpause) );
185 auto node = getChildByTag( kTagGrossini );
186 auto director = Director::getInstance();
187 director->getActionManager()->resumeTarget(node);
188}
189
190std::string PauseTest::subtitle() const
191{
192 return "Pause Test";
193}
194
195//------------------------------------------------------------------
196//
197// RemoveTest
198//
199//------------------------------------------------------------------
201{
203
204 auto l = Label::createWithTTF("Should not crash", "fonts/Thonburi.ttf", 16.0f);
205 addChild(l);
206 l->setPosition(VisibleRect::center().x, VisibleRect::top().y - 75);
207
208 auto pMove = MoveBy::create(2, Vec2(200.0f, 0.0f));
209 auto pCallback = CallFunc::create(CC_CALLBACK_0(StopActionTest::stopAction,this));
210 auto pSequence = Sequence::create(pMove, pCallback, nullptr);
211 pSequence->setTag(kTagSequence);
212
213 auto pChild = Sprite::create(s_pathGrossini);
214 pChild->setPosition( VisibleRect::center() );
215
216 addChild(pChild, 1, kTagGrossini);
217 pChild->runAction(pSequence);
218}
219
221{
222 auto sprite = getChildByTag(kTagGrossini);
223 sprite->stopActionByTag(kTagSequence);
224}
225
226std::string StopActionTest::subtitle() const
227{
228 return "Stop Action Test";
229}
230
231//------------------------------------------------------------------
232//
233// RemoveTest
234//
235//------------------------------------------------------------------
237{
239
240 auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "fonts/Thonburi.ttf", 16.0f);
241 addChild(l);
242 l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 75) );
243
244 auto pMove1 = MoveBy::create(2, Vec2(200.0f, 0.0f));
245 auto pMove2 = MoveBy::create(2, Vec2(-200.0f, 0.0f));
246 auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
247 auto pRepeatMove = RepeatForever::create(pSequenceMove);
248 pRepeatMove->setTag(kTagSequence);
249
250 auto pScale1 = ScaleBy::create(2, 1.5f);
251 auto pScale2 = ScaleBy::create(2, 1.0f/1.5f);
252 auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
253 auto pRepeatScale = RepeatForever::create(pSequenceScale);
254 pRepeatScale->setTag(kTagSequence);
255
256 auto pRotate = RotateBy::create(2, 360);
257 auto pRepeatRotate = RepeatForever::create(pRotate);
258
259 auto pChild = Sprite::create(s_pathGrossini);
260 pChild->setPosition( VisibleRect::center() );
261
262 addChild(pChild, 1, kTagGrossini);
263 pChild->runAction(pRepeatMove);
264 pChild->runAction(pRepeatScale);
265 pChild->runAction(pRepeatRotate);
266 this->scheduleOnce((SEL_SCHEDULE)&StopAllActionsTest::stopAction, 4);
267}
268
270{
271 auto sprite = getChildByTag(kTagGrossini);
272 sprite->stopAllActionsByTag(kTagSequence);
273}
274
276{
277 return "Stop All Action Test";
278}
279
280
281//------------------------------------------------------------------
282//
283// ResumeTest
284//
285//------------------------------------------------------------------
286std::string ResumeTest::subtitle() const
287{
288 return "Resume Test";
289}
290
292{
294
295 auto l = Label::createWithTTF("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi.ttf", 16.0f);
296 addChild(l);
297 l->setPosition(VisibleRect::center().x, VisibleRect::top().y - 75);
298
299 auto pGrossini = Sprite::create(s_pathGrossini);
300 addChild(pGrossini, 0, kTagGrossini);
301 pGrossini->setPosition(VisibleRect::center());
302
303 pGrossini->runAction(ScaleBy::create(2, 2));
304
305 auto director = Director::getInstance();
306 director->getActionManager()->pauseTarget(pGrossini);
307 pGrossini->runAction(RotateBy::create(2, 360));
308
309 this->schedule(CC_SCHEDULE_SELECTOR(ResumeTest::resumeGrossini), 3.0f);
310}
311
313{
314 this->unschedule(CC_SCHEDULE_SELECTOR(ResumeTest::resumeGrossini));
315
316 auto pGrossini = getChildByTag(kTagGrossini);
317 auto director = Director::getInstance();
318 director->getActionManager()->resumeTarget(pGrossini);
319}
320
321//------------------------------------------------------------------
322//
323// StopActionsByFlagsTest
324//
325//------------------------------------------------------------------
327{
329
330 auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "fonts/Thonburi.ttf", 16.0f);
331 addChild(l);
332 l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 75) );
333
334 auto pMove1 = MoveBy::create(2, Vec2(200.0f, 0.0f));
335 auto pMove2 = MoveBy::create(2, Vec2(-200.0f, 0.0f));
336 auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
337 auto pRepeatMove = RepeatForever::create(pSequenceMove);
338 pRepeatMove->setFlags(kMoveFlag | kRepeatForeverFlag);
339
340 auto pScale1 = ScaleBy::create(2, 1.5f);
341 auto pScale2 = ScaleBy::create(2, 1.0f/1.5f);
342 auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
343 auto pRepeatScale = RepeatForever::create(pSequenceScale);
344 pRepeatScale->setFlags(kScaleFlag | kRepeatForeverFlag);
345
346 auto pRotate = RotateBy::create(2, 360);
347 auto pRepeatRotate = RepeatForever::create(pRotate);
348 pRepeatRotate->setFlags(kRotateFlag | kRepeatForeverFlag);
349
350 auto pChild = Sprite::create(s_pathGrossini);
351 pChild->setPosition( VisibleRect::center() );
352
353 addChild(pChild, 1, kTagGrossini);
354 pChild->runAction(pRepeatMove);
355 pChild->runAction(pRepeatScale);
356 pChild->runAction(pRepeatRotate);
357 this->scheduleOnce((SEL_SCHEDULE)&StopActionsByFlagsTest::stopAction, 4);
358}
359
361{
362 auto sprite = getChildByTag(kTagGrossini);
363 sprite->stopActionsByFlags(kMoveFlag | kScaleFlag);
364}
365
367{
368 return "Stop All Actions By Flags Test";
369}
370
371//------------------------------------------------------------------
372//
373// Issue14050Test
374//
375//------------------------------------------------------------------
376class SpriteIssue14050: public Sprite
377{
378public:
380 {
381 log("SpriteIssue14050::constructor");
382 }
384 {
385 log("SpriteIssue14050::destructor");
386 }
387};
388
390{
392
393 auto sprite = new (std::nothrow) SpriteIssue14050;
394 sprite->initWithFile("Images/grossini.png");
395 sprite->autorelease();
396
397 auto move = MoveBy::create(2, Vec2(100.0f, 100.0f));
398 sprite->runAction(move);
399}
400
401std::string Issue14050Test::subtitle() const
402{
403 return "Issue14050. Sprite should not leak.";
404}
@ kTagGrossini
@ kTagSequence
@ kTagNode
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
void bugMe(Node *node)
virtual std::string subtitle() const override
virtual void onEnter() override
void unpause(float dt)
virtual std::string subtitle() const override
void resumeGrossini(float time)
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void onEnter() override
const unsigned int kRepeatForeverFlag
const unsigned int kScaleFlag
const unsigned int kMoveFlag
const unsigned int kRotateFlag
void stopAction(float time)
virtual void onEnter() override
virtual std::string subtitle() const override
TestSuite * getTestSuite() const
Definition: BaseTest.h:83
virtual void onEnter() override
Definition: BaseTest.cpp:430
virtual void enterNextTest()
Definition: BaseTest.cpp:309
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static const char s_pathGrossini[]
Definition: testResource.h:28