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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Bug-Child.cpp
|
|
|
|
// cocos2d_tests
|
|
|
|
//
|
|
|
|
// Created by NiTe Luo on 5/12/14.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Bug-Child.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
bool BugChild::init()
|
|
|
|
{
|
|
|
|
if (BugsTestBase::init())
|
|
|
|
{
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// create and initialize a Label
|
|
|
|
auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugChild::switchChild, this));
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
menu = Menu::create(item1, nullptr);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
menu->alignItemsVertically();
|
2021-12-28 21:27:32 +08:00
|
|
|
menu->setPosition(size.width / 2, 100);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(menu);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
parent1 = Sprite::create("Images/grossini.png");
|
2021-12-28 21:27:32 +08:00
|
|
|
parent1->setPosition(size.width / 4, size.height / 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(parent1);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
parent2 = Sprite::create("Images/grossinis_sister1.png");
|
2021-12-28 21:27:32 +08:00
|
|
|
parent2->setPosition(size.width * 3 / 4, size.height / 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(parent2);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
child = Sprite::create("Images/grossinis_sister2.png");
|
|
|
|
child->setPosition(20, 20);
|
|
|
|
child->retain();
|
|
|
|
parent1->addChild(child);
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void BugChild::switchChild(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
if (parent1->getChildrenCount() > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
parent1->removeChild(child, false);
|
|
|
|
parent2->addChild(child);
|
|
|
|
CCLOG("Child attached to parent2");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent2->removeChild(child, false);
|
|
|
|
parent1->addChild(child);
|
|
|
|
CCLOG("Child attached to parent1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BugCameraMask::init()
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
if (!BugsTestBase::init())
|
|
|
|
return false;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto node = Node::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
node->setPosition(size.width / 4, size.height / 3);
|
2019-11-23 20:27:39 +08:00
|
|
|
_sprite = Sprite::create("Images/grossini.png");
|
|
|
|
node->addChild(_sprite);
|
|
|
|
node->setCameraMask((unsigned short)CameraFlag::USER1);
|
2021-12-28 21:27:32 +08:00
|
|
|
auto move = MoveBy::create(2, Vec2(200, 0));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
node->runAction(RepeatForever::create(Sequence::createWithTwoActions(move, move->reverse())));
|
|
|
|
addChild(node);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto camera = Camera::create();
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
addChild(camera);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugCameraMask::switchSpriteFlag, this));
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto menu = Menu::create(item1, nullptr);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
menu->alignItemsVertically();
|
2021-12-28 21:27:32 +08:00
|
|
|
menu->setPosition(size.width / 2, 100);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(menu);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_spriteMaskLabel = Label::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
_spriteMaskLabel->setPosition(size.width / 2, 120);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(_spriteMaskLabel);
|
|
|
|
updateSpriteMaskLabel();
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto label = Label::create();
|
2021-12-28 21:27:32 +08:00
|
|
|
label->setPosition(size.width / 2, size.height * 0.9f);
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("Sprite should always run action.");
|
|
|
|
addChild(label);
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void BugCameraMask::switchSpriteFlag(Ref* sender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
if ((unsigned short)CameraFlag::USER1 == _sprite->getCameraMask())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
}
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateSpriteMaskLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BugCameraMask::updateSpriteMaskLabel()
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
2021-12-28 21:27:32 +08:00
|
|
|
stream << "The camera Mask is " << (_sprite->getCameraMask() == 1 ? "CameraFlag::Default" : "CameraFlag::User1")
|
|
|
|
<< std::endl;
|
2019-11-23 20:27:39 +08:00
|
|
|
_spriteMaskLabel->setString(stream.str());
|
|
|
|
}
|