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

61 lines
1.4 KiB
C++
Raw Normal View History

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_CC;
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();
// create and initialize a Label
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);
parent1 = Sprite::create("Images/grossini.png");
parent1->setPosition(size.width/4, size.height/2);
2014-05-13 13:50:31 +08:00
addChild(parent1);
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);
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)
{
parent1->removeChild(child, false);
2014-05-13 13:50:31 +08:00
parent2->addChild(child);
CCLOG("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);
CCLOG("Child attached to parent1");
2014-05-13 13:50:31 +08:00
}
}