2012-04-19 14:35:52 +08:00
|
|
|
|
#include "HelloWorldScene.h"
|
2012-10-11 18:33:43 +08:00
|
|
|
|
#include "AppMacros.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
#include "event_dispatcher/CCTouchEventListener.h"
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
2012-10-11 18:33:43 +08:00
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
|
Scene* HelloWorld::scene()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// 'scene' is an autorelease object
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto scene = Scene::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// 'layer' is an autorelease object
|
2012-06-14 15:13:16 +08:00
|
|
|
|
HelloWorld *layer = HelloWorld::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// add layer as a child to scene
|
|
|
|
|
scene->addChild(layer);
|
|
|
|
|
|
|
|
|
|
// return the scene
|
|
|
|
|
return scene;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// on "init" you need to initialize your instance
|
|
|
|
|
bool HelloWorld::init()
|
|
|
|
|
{
|
|
|
|
|
//////////////////////////////
|
|
|
|
|
// 1. super init first
|
2013-06-20 14:17:10 +08:00
|
|
|
|
if ( !Layer::init() )
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-08-07 14:29:46 +08:00
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
|
// 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
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto closeItem = MenuItemImage::create(
|
2012-04-19 14:35:52 +08:00
|
|
|
|
"CloseNormal.png",
|
|
|
|
|
"CloseSelected.png",
|
2013-06-25 07:59:00 +08:00
|
|
|
|
CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
|
2012-08-16 10:21:15 +08:00
|
|
|
|
|
2013-07-31 14:38:50 +08:00
|
|
|
|
closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));
|
2013-09-03 18:22:03 +08:00
|
|
|
|
// closeItem->setRotation(30);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// create menu, it's an autorelease object
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto menu = Menu::create(closeItem, NULL);
|
2013-07-24 06:20:22 +08:00
|
|
|
|
menu->setPosition(Point::ZERO);
|
|
|
|
|
this->addChild(menu, 1);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
// closeItem->setVisible(false);
|
|
|
|
|
|
|
|
|
|
auto closeItem2 = MenuItemImage::create(
|
|
|
|
|
"CloseNormal.png",
|
|
|
|
|
"CloseSelected.png",
|
|
|
|
|
CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
|
|
|
|
|
|
|
|
|
|
closeItem2->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2) - Point(0, 20));
|
|
|
|
|
// closeItem2->setRotation(30);
|
|
|
|
|
menu->addChild(closeItem2, 100);
|
|
|
|
|
// create menu, it's an autorelease object
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
// closeItem->unregisterEventCallback(touchEventId);
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
/////////////////////////////
|
|
|
|
|
// 3. add your codes below...
|
|
|
|
|
|
|
|
|
|
// add a label shows "Hello World"
|
|
|
|
|
// create and initialize a label
|
2012-10-11 18:33:43 +08:00
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
|
2012-10-11 18:33:43 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// position the label on the center of the screen
|
2013-07-24 06:20:22 +08:00
|
|
|
|
label->setPosition(Point(origin.x + visibleSize.width/2,
|
|
|
|
|
origin.y + visibleSize.height - label->getContentSize().height));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// add the label as a child to this layer
|
2013-07-24 06:20:22 +08:00
|
|
|
|
this->addChild(label, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// add "HelloWorld" splash screen"
|
2013-08-16 16:05:27 +08:00
|
|
|
|
auto sprite = Sprite::create("HelloWorld.png");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// position the sprite on the center of the screen
|
2013-07-31 14:38:50 +08:00
|
|
|
|
sprite->setPosition(Point(visibleSize / 2) + origin);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// add the sprite as a child to this layer
|
2013-09-03 18:22:03 +08:00
|
|
|
|
this->addChild(sprite);
|
|
|
|
|
|
|
|
|
|
auto dispatcher = EventDispatcher::getInstance();
|
|
|
|
|
|
|
|
|
|
auto layerTouchListener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE);
|
|
|
|
|
layerTouchListener->onTouchesBegan = [=](const std::vector<Touch*>& touches, Event* event){
|
|
|
|
|
CCLOG("layer touches began... count = %d", (int)touches.size());
|
2013-09-12 20:47:15 +08:00
|
|
|
|
// dispatcher->removeAllListeners();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
};
|
|
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
|
auto layerTouchListener2 = layerTouchListener->clone();
|
|
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener, this);
|
|
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener2, this);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
auto spriteTouchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
|
spriteTouchListener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
|
|
spriteTouchListener->onTouchBegan = [=](Touch* touch, Event* evt){
|
|
|
|
|
CCLOG("Touch sprite.... began... %d, drawOrder = %d", sprite->getZOrder(), sprite->getEventPriority());
|
2013-09-14 09:02:49 +08:00
|
|
|
|
dispatcher->removeEventListener(layerTouchListener);
|
|
|
|
|
dispatcher->removeEventListener(layerTouchListener2);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(spriteTouchListener, sprite);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
|
int zorder = rand() % 50;
|
|
|
|
|
auto sprite1 = Sprite::create("CloseNormal.png");
|
|
|
|
|
|
|
|
|
|
// position the sprite on the center of the screen
|
|
|
|
|
sprite1->setPosition(Point(30*(i+1), visibleSize.height/2) + origin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (zorder % 2 == 0)
|
|
|
|
|
{
|
|
|
|
|
// add the sprite as a child to this layer
|
|
|
|
|
this->addChild(sprite1, zorder);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sprite->addChild(sprite1, zorder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buf[100] = {0};
|
|
|
|
|
sprintf(buf, "%d", zorder);
|
|
|
|
|
auto label = LabelTTF::create(buf, "", 16);
|
|
|
|
|
sprite1->addChild(label);
|
|
|
|
|
|
|
|
|
|
auto spriteItemTouchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
|
spriteItemTouchListener->onTouchBegan = [=](Touch* touch, Event* event){
|
|
|
|
|
Rect hitRect;
|
|
|
|
|
hitRect.size = sprite1->getContentSize();
|
|
|
|
|
|
|
|
|
|
Point localPoint = sprite1->convertToNodeSpace(touch->getLocation());
|
|
|
|
|
// HitTest
|
|
|
|
|
if (!hitRect.containsPoint(localPoint))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
CCLOG("Touch sprite222.... began..zorder: %d. drawOrder: %d", sprite1->getZOrder(), sprite1->getEventPriority());
|
|
|
|
|
sprite1->setColor(Color3B::BLACK);
|
2013-09-12 20:47:15 +08:00
|
|
|
|
event->stopPropagation();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
spriteItemTouchListener->onTouchEnded = [=](Touch* touch, Event* event) {
|
|
|
|
|
sprite1->setColor(Color3B::WHITE);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
spriteItemTouchListener->setSwallowTouches(true);
|
|
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(spriteItemTouchListener, sprite1);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// EventDispatcher::getInstance()->registerEventListener(TouchEvent::EVENT_TYPE, [=](Event* evt){
|
|
|
|
|
// TouchEvent* touchEvent = static_cast<TouchEvent*>(evt);
|
|
|
|
|
// if (touchEvent->getEventCode() == TouchEvent::EventCode::BEGAN)
|
|
|
|
|
// {
|
|
|
|
|
// CCLOG("Touch sprite333.... began..zorder: %d. drawOrder: %d", sprite1->getZOrder(), sprite1->getEventPriority());
|
|
|
|
|
// }
|
|
|
|
|
// }, zorder);
|
|
|
|
|
|
|
|
|
|
CCLOG("set zorder : %d", zorder);
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
|
void HelloWorld::menuCloseCallback(Object* sender)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-09-12 17:31:37 +08:00
|
|
|
|
Director::getInstance()->end();
|
|
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
exit(0);
|
|
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|