PuzzleSDK
Bug-Child.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//
26// Bug-Child.cpp
27// cocos2d_tests
28//
29// Created by NiTe Luo on 5/12/14.
30//
31//
32
33#include "Bug-Child.h"
34
36
38{
39 if (BugsTestBase::init())
40 {
41 auto size = Director::getInstance()->getWinSize();
42
43 // create and initialize a Label
44 auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugChild::switchChild, this));
45
46 menu = Menu::create(item1, nullptr);
47
48 menu->alignItemsVertically();
49 menu->setPosition(size.width/2, 100);
50 addChild(menu);
51
52 parent1 = Sprite::create("Images/grossini.png");
53 parent1->setPosition(size.width/4, size.height/2);
54 addChild(parent1);
55
56 parent2 = Sprite::create("Images/grossinis_sister1.png");
57 parent2->setPosition(size.width*3/4, size.height/2);
58 addChild(parent2);
59
60 child = Sprite::create("Images/grossinis_sister2.png");
61 child->setPosition(20, 20);
62 child->retain();
63 parent1->addChild(child);
64 return true;
65 }
66
67 return false;
68}
69
70void BugChild::switchChild(Ref *sender)
71{
72 if(parent1->getChildrenCount() > 0)
73 {
74 parent1->removeChild(child, false);
75 parent2->addChild(child);
76 CCLOG("Child attached to parent2");
77 }
78 else
79 {
80 parent2->removeChild(child, false);
81 parent1->addChild(child);
82 CCLOG("Child attached to parent1");
83 }
84}
85
87{
88 if (!BugsTestBase::init()) return false;
89
90 auto size = Director::getInstance()->getWinSize();
91
92 auto node = Node::create();
93 node->setPosition(size.width/4, size.height/3);
94 _sprite = Sprite::create("Images/grossini.png");
95 node->addChild(_sprite);
96 node->setCameraMask((unsigned short)CameraFlag::USER1);
97 auto move = MoveBy::create(2, Vec2(200,0));
98
99 node->runAction(RepeatForever::create(Sequence::createWithTwoActions(move, move->reverse())));
100 addChild(node);
101
102 auto camera = Camera::create();
103 camera->setCameraFlag(CameraFlag::USER1);
104 addChild(camera);
105
106
107 auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugCameraMask::switchSpriteFlag, this));
108
109 auto menu = Menu::create(item1, nullptr);
110
111 menu->alignItemsVertically();
112 menu->setPosition(size.width/2, 100);
113 addChild(menu);
114
115 _spriteMaskLabel = Label::create();
116 _spriteMaskLabel->setPosition(size.width/2, 120);
117 addChild(_spriteMaskLabel);
119
120 auto label = Label::create();
121 label->setPosition(size.width/2, size.height * 0.9f);
122 label->setString("Sprite should always run action.");
123 addChild(label);
124
125 return true;
126}
127
129{
130 if((unsigned short) CameraFlag::USER1 == _sprite->getCameraMask())
131 {
132 _sprite->setCameraMask((unsigned short)CameraFlag::DEFAULT);
133 }
134 else
135 {
136 _sprite->setCameraMask((unsigned short)CameraFlag::USER1);
137 }
138
140}
141
143{
144 std::stringstream stream;
145 stream << "The camera Mask is " << (_sprite->getCameraMask() == 1 ? "CameraFlag::Default" : "CameraFlag::User1") << std::endl;
146 _spriteMaskLabel->setString(stream.str());
147}
USING_NS_CC
Definition: Bug-Child.cpp:35
void updateSpriteMaskLabel()
Definition: Bug-Child.cpp:142
virtual bool init() override
Definition: Bug-Child.cpp:86
Node * _sprite
Definition: Bug-Child.h:68
cocos2d::Label * _spriteMaskLabel
Definition: Bug-Child.h:69
void switchSpriteFlag(cocos2d::Ref *sender)
Definition: Bug-Child.cpp:128
virtual bool init() override
Definition: Bug-Child.cpp:37
cocos2d::Sprite * parent1
Definition: Bug-Child.h:50
cocos2d::Menu * menu
Definition: Bug-Child.h:55
cocos2d::Sprite * child
Definition: Bug-Child.h:53
void switchChild(cocos2d::Ref *sender)
Definition: Bug-Child.cpp:70
cocos2d::Sprite * parent2
Definition: Bug-Child.h:51