// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
auto button = cocos2d::ui::Button::create("HelloWorld.png");
button->setPosition(Vec2(origin.x + visibleSize.width/4,
origin.y + visibleSize.height - label->getContentSize().height));
button->addClickEventListener(CC_CALLBACK_1(HelloWorld::pp, this));
this->addChild(button);
schedule(schedule_selector(HelloWorld::handUpdate), 1.f, 10, 5);
auto redLayer = LayerColor::create(cocos2d::Color4B(100, 200, 300, 255), visibleSize.width/2, visibleSize.height/2);
redLayer->setTag(200);
redLayer->setIgnoreAnchorPointForPosition(false);
redLayer->setAnchorPoint(cocos2d::Vec2(0.5f, 0.5f));
redLayer->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
redLayer->setPositionZ(-80.f);
this->addChild(redLayer, 0);
redLayer->setLocalZOrder(1);
redLayer->setGlobalZOrder(1);
auto greenLayer = LayerColor::create(cocos2d::Color4B(200, 200, 255, 255), visibleSize.width/4, visibleSize.height/4);
greenLayer->setTag(100);
greenLayer->setIgnoreAnchorPointForPosition(false);
greenLayer->setAnchorPoint(cocos2d::Vec2(0.5f, 0.5f));
greenLayer->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2-50);
greenLayer->setPositionZ(0);
this->addChild(greenLayer, 1);
greenLayer->setLocalZOrder(0);
greenLayer->setGlobalZOrder(0);
auto listener1 = EventListenerTouchOneByOne::create();
listener1->onTouchBegan = [](Touch* touch, Event* event)
{
// 获取事件所绑定的 target
auto target = static_cast<Node*>(event->getCurrentTarget());
// 获取当前点击点所在相对按钮的位置坐标
Point pt = touch->getLocation();
Point locationInNode = target->convertToNodeSpace(pt);
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite1 began... x = %f, y = %f", locationInNode.x, locationInNode.y);
target->setOpacity(180);
return true;
}
log("next pass1");
return false;
};
listener1->setSwallowTouches(false);
auto listener2 = EventListenerTouchOneByOne::create();
listener2->onTouchBegan = [](Touch* touch, Event* event)
{
// 获取事件所绑定的 target
auto target = static_cast<Node*>(event->getCurrentTarget());
// 获取当前点击点所在相对按钮的位置坐标
Point pt = touch->getLocation();
Point locationInNode = target->convertToNodeSpace(pt);
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
target->setOpacity(180);
return true;
}
log("next pass");
return false;
};
listener2->setSwallowTouches(true);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, redLayer);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, greenLayer);
return true;
}
网友评论