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();
|
|
|
|
|
|
|
|
// 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));
|
2014-05-13 13:50:31 +08:00
|
|
|
|
2014-05-13 16:43:19 +08:00
|
|
|
menu = Menu::create(item1, nullptr);
|
2014-05-13 13:50:31 +08:00
|
|
|
|
|
|
|
menu->alignItemsVertically();
|
|
|
|
menu->setPosition(size.width/2, 100);
|
|
|
|
addChild(menu);
|
|
|
|
|
2014-05-13 14:28:20 +08:00
|
|
|
parent1 = Sprite::create("Images/grossini.png");
|
|
|
|
parent1->setPosition(size.width/4, size.height/2);
|
2014-05-13 13:50:31 +08:00
|
|
|
addChild(parent1);
|
|
|
|
|
2014-05-13 14:28:20 +08:00
|
|
|
parent2 = Sprite::create("Images/grossinis_sister1.png");
|
|
|
|
parent2->setPosition(size.width*3/4, size.height/2);
|
2014-05-13 13:50:31 +08:00
|
|
|
addChild(parent2);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BugChild::switchChild(Ref *sender)
|
|
|
|
{
|
|
|
|
if(parent1->getChildrenCount() > 0)
|
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
if (!BugsTestBase::init()) return false;
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setPosition(size.width/4, size.height/3);
|
|
|
|
_sprite = Sprite::create("Images/grossini.png");
|
|
|
|
node->addChild(_sprite);
|
|
|
|
node->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
auto move = MoveBy::create(2, Vec2(200,0));
|
|
|
|
|
|
|
|
node->runAction(RepeatForever::create(Sequence::createWithTwoActions(move, move->reverse())));
|
|
|
|
addChild(node);
|
|
|
|
|
|
|
|
auto camera = Camera::create();
|
|
|
|
camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
addChild(camera);
|
|
|
|
|
|
|
|
|
|
|
|
auto item1 = MenuItemFont::create("Switch Child", CC_CALLBACK_1(BugCameraMask::switchSpriteFlag, this));
|
|
|
|
|
|
|
|
auto menu = Menu::create(item1, nullptr);
|
|
|
|
|
|
|
|
menu->alignItemsVertically();
|
|
|
|
menu->setPosition(size.width/2, 100);
|
|
|
|
addChild(menu);
|
|
|
|
|
|
|
|
_spriteMaskLabel = Label::create();
|
|
|
|
_spriteMaskLabel->setPosition(size.width/2, 120);
|
|
|
|
addChild(_spriteMaskLabel);
|
|
|
|
updateSpriteMaskLabel();
|
|
|
|
|
|
|
|
auto label = Label::create();
|
|
|
|
label->setPosition(size.width/2, size.height * 0.9);
|
|
|
|
label->setString("Sprite should always run action.");
|
|
|
|
addChild(label);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BugCameraMask::switchSpriteFlag(Ref *sender)
|
|
|
|
{
|
|
|
|
if((unsigned short) CameraFlag::USER1 == _sprite->getCameraMask())
|
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSpriteMaskLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BugCameraMask::updateSpriteMaskLabel()
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
stream<<"The camera Mask is "<<(_sprite->getCameraMask() == 1 ? "CamereFlag::Default" : "CameraFlag::User1")<<std::endl;
|
|
|
|
_spriteMaskLabel->setString(stream.str());
|
|
|
|
}
|