axmol/tests/cpp-tests/Classes/BugsTest/Bug-Child.cpp

149 lines
4.4 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2021-12-28 16:06:23 +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
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
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"
USING_NS_AX;
2014-05-13 13:50:31 +08:00
bool BugChild::init()
{
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
2022-07-15 19:17:01 +08:00
auto item1 = MenuItemFont::create("Switch Child", AX_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
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
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
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
{
parent1->removeChild(child, false);
2014-05-13 13:50:31 +08:00
parent2->addChild(child);
2022-07-16 10:43:05 +08:00
AXLOG("Child attached to parent2");
2014-05-13 13:50:31 +08:00
}
else
{
parent2->removeChild(child, false);
2014-05-13 13:50:31 +08:00
parent1->addChild(child);
2022-07-16 10:43:05 +08:00
AXLOG("Child attached to parent1");
2014-05-13 13:50:31 +08:00
}
}
bool BugCameraMask::init()
{
2021-12-28 16:06:23 +08:00
if (!BugsTestBase::init())
return false;
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
auto node = Node::create();
2021-12-28 16:06:23 +08:00
node->setPosition(size.width / 4, size.height / 3);
_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));
node->runAction(RepeatForever::create(Sequence::createWithTwoActions(move, move->reverse())));
addChild(node);
2021-12-28 16:06:23 +08:00
auto camera = Camera::create();
camera->setCameraFlag(CameraFlag::USER1);
addChild(camera);
2021-12-28 16:06:23 +08:00
2022-07-15 19:17:01 +08:00
auto item1 = MenuItemFont::create("Switch Child", AX_CALLBACK_1(BugCameraMask::switchSpriteFlag, this));
2021-12-28 16:06:23 +08:00
auto menu = Menu::create(item1, nullptr);
2021-12-28 16:06:23 +08:00
menu->alignItemsVertically();
2021-12-28 16:06:23 +08:00
menu->setPosition(size.width / 2, 100);
addChild(menu);
2021-12-28 16:06:23 +08:00
_spriteMaskLabel = Label::create();
2021-12-28 16:06:23 +08:00
_spriteMaskLabel->setPosition(size.width / 2, 120);
addChild(_spriteMaskLabel);
updateSpriteMaskLabel();
2021-12-28 16:06:23 +08:00
auto label = Label::create();
2021-12-28 16:06:23 +08:00
label->setPosition(size.width / 2, size.height * 0.9f);
label->setString("Sprite should always run action.");
addChild(label);
2021-12-28 16:06:23 +08:00
return true;
}
2021-12-28 16:06:23 +08:00
void BugCameraMask::switchSpriteFlag(Ref* sender)
{
2021-12-28 16:06:23 +08:00
if ((unsigned short)CameraFlag::USER1 == _sprite->getCameraMask())
{
_sprite->setCameraMask((unsigned short)CameraFlag::DEFAULT);
}
else
{
_sprite->setCameraMask((unsigned short)CameraFlag::USER1);
}
2021-12-28 16:06:23 +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;
_spriteMaskLabel->setString(stream.str());
}