2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-13 13:50:31 +08:00
|
|
|
//
|
|
|
|
// Bug-Child.cpp
|
|
|
|
// cocos2d_tests
|
|
|
|
//
|
|
|
|
// Created by NiTe Luo on 5/12/14.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Bug-Child.h"
|
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2014-05-13 13:50:31 +08:00
|
|
|
bool BugChild::init()
|
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if (BugsTestBase::init())
|
2014-05-13 13:50:31 +08:00
|
|
|
{
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 13:50:31 +08:00
|
|
|
// create and initialize a Label
|
2014-05-13 14:28:20 +08:00
|
|
|
auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugChild::switchChild, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 16:43:19 +08:00
|
|
|
menu = Menu::create(item1, nullptr);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 13:50:31 +08:00
|
|
|
menu->alignItemsVertically();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(size.width / 2, 100);
|
2014-05-13 13:50:31 +08:00
|
|
|
addChild(menu);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 14:28:20 +08:00
|
|
|
parent1 = Sprite::create("Images/grossini.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
parent1->setPosition(size.width / 4, size.height / 2);
|
2014-05-13 13:50:31 +08:00
|
|
|
addChild(parent1);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 14:28:20 +08:00
|
|
|
parent2 = Sprite::create("Images/grossinis_sister1.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
parent2->setPosition(size.width * 3 / 4, size.height / 2);
|
2014-05-13 13:50:31 +08:00
|
|
|
addChild(parent2);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 14:28:20 +08:00
|
|
|
child = Sprite::create("Images/grossinis_sister2.png");
|
|
|
|
child->setPosition(20, 20);
|
|
|
|
child->retain();
|
|
|
|
parent1->addChild(child);
|
2014-05-13 13:50:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-13 13:50:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void BugChild::switchChild(Ref* sender)
|
2014-05-13 13:50:31 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (parent1->getChildrenCount() > 0)
|
2014-05-13 13:50:31 +08:00
|
|
|
{
|
2014-05-13 14:28:20 +08:00
|
|
|
parent1->removeChild(child, false);
|
2014-05-13 13:50:31 +08:00
|
|
|
parent2->addChild(child);
|
2014-05-13 14:28:20 +08:00
|
|
|
CCLOG("Child attached to parent2");
|
2014-05-13 13:50:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-13 14:28:20 +08:00
|
|
|
parent2->removeChild(child, false);
|
2014-05-13 13:50:31 +08:00
|
|
|
parent1->addChild(child);
|
2014-05-13 14:28:20 +08:00
|
|
|
CCLOG("Child attached to parent1");
|
2014-05-13 13:50:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-21 16:57:13 +08:00
|
|
|
|
|
|
|
bool BugCameraMask::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!BugsTestBase::init())
|
|
|
|
return false;
|
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto node = Node::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
node->setPosition(size.width / 4, size.height / 3);
|
2015-05-21 16:57:13 +08:00
|
|
|
_sprite = Sprite::create("Images/grossini.png");
|
|
|
|
node->addChild(_sprite);
|
|
|
|
node->setCameraMask((unsigned short)CameraFlag::USER1);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto move = MoveBy::create(2, Vec2(200, 0));
|
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
node->runAction(RepeatForever::create(Sequence::createWithTwoActions(move, move->reverse())));
|
|
|
|
addChild(node);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto camera = Camera::create();
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
addChild(camera);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugCameraMask::switchSpriteFlag, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto menu = Menu::create(item1, nullptr);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
menu->alignItemsVertically();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(size.width / 2, 100);
|
2015-05-21 16:57:13 +08:00
|
|
|
addChild(menu);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
_spriteMaskLabel = Label::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_spriteMaskLabel->setPosition(size.width / 2, 120);
|
2015-05-21 16:57:13 +08:00
|
|
|
addChild(_spriteMaskLabel);
|
|
|
|
updateSpriteMaskLabel();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
auto label = Label::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
label->setPosition(size.width / 2, size.height * 0.9f);
|
2015-05-21 16:57:13 +08:00
|
|
|
label->setString("Sprite should always run action.");
|
|
|
|
addChild(label);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void BugCameraMask::switchSpriteFlag(Ref* sender)
|
2015-05-21 16:57:13 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if ((unsigned short)CameraFlag::USER1 == _sprite->getCameraMask())
|
2015-05-21 16:57:13 +08:00
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-05-21 16:57:13 +08:00
|
|
|
updateSpriteMaskLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BugCameraMask::updateSpriteMaskLabel()
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
2021-12-28 16:06:23 +08:00
|
|
|
stream << "The camera Mask is " << (_sprite->getCameraMask() == 1 ? "CameraFlag::Default" : "CameraFlag::User1")
|
|
|
|
<< std::endl;
|
2015-05-21 16:57:13 +08:00
|
|
|
_spriteMaskLabel->setString(stream.str());
|
2015-05-21 17:12:35 +08:00
|
|
|
}
|