#include "HelloWorldScene.h"
USING_NS_CC;
using namespace gui;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// 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
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point 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(Point(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(Point::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = LabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
label->setPosition(Point(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,11);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
setTouchEnabled(true);
//设置为单点响应
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
///////////////////////////////////////////////////////
auto moveSp = Sprite::create("things.png");
moveSp->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->setTag(1);
this->addChild(moveSp);
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto myListener = EventListenerTouchOneByOne::create();
//如果不加入此句消息依旧会向下传递
myListener->setSwallowTouches(true);
myListener->onTouchBegan = [=](Touch* touch,Event* event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
target->setOpacity(180);
return true;
}
return false;
};
myListener->onTouchMoved = [=](Touch* touch,Event* event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition()+touch->getDelta());
};
myListener->onTouchEnded = [=](Touch* touch,Event* event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());
if(target)
{
target->setOpacity(255);
}
};
dispatcher->addEventListenerWithSceneGraphPriority(myListener,moveSp);
/////////////////////////////////////
Button* button = Button::create();
button->setTouchEnabled(true);
button->loadTextures("button.png","button_pressed.png","");
button->setPosition(Point(visibleSize.width + origin.x - 200, visibleSize.height/2 + origin.y));
button->addTouchEventListener(this,toucheventselector(HelloWorld::touchButton));
this->addChild(button);
return true;
}
void HelloWorld::touchButton(Object* object,TouchEventType type)
{
LabelTTF* label;
switch (type)
{
case TouchEventType::TOUCH_EVENT_BEGAN:
label = static_cast<LabelTTF*>(getChildByTag(11));
label->setString("按下按钮");
break;
case TouchEventType::TOUCH_EVENT_MOVED:
label = static_cast<LabelTTF*>(getChildByTag(11));
label->setString("按下按钮移动");
break;
case TouchEventType::TOUCH_EVENT_ENDED:
label = static_cast<LabelTTF*>(getChildByTag(11));
label->setString("放开按钮");
break;
case TouchEventType::TOUCH_EVENT_CANCELED:
label = static_cast<LabelTTF*>(getChildByTag(11));
label->setString("取消点击");
break;
default:
break;
}
}
bool HelloWorld::onTouchBegan(Touch* touch, Event *event)
{
return true;
}
void HelloWorld::onTouchMoved(Touch* touch, Event *event)
{
this->setPosition(this->getPosition()+touch->getDelta());
}
void HelloWorld::onTouchEnded(Touch* touch, Event *event)
{
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
Cocos2d-x 3.0开发(三)点击交互的四种处理beta2版代码
5星 · 超过95%的资源 需积分: 9 180 浏览量
2014-01-30
10:50:10
上传
评论
收藏 286KB ZIP 举报
松阳
- 粉丝: 2601
- 资源: 22
最新资源
- 毕业设计-仿生六足机器人的制作全教程源码+电子元器件+程序代码+线路组件图+安装教程+搭建视频教程
- 基于ROS和webots的xrobot机械臂仿真初探C++源码
- 基于ROS的点焊机器人仿真与控制python源码+文档说明+使用说明+详细注释
- 基于vue实现的细粒度交通时空大数据分析系统+源代码+文档说明
- 安卓大作业-基于Electron的交通时空大数据分析挖掘系统客户端(Android)+源代码+文档说明+界面截图
- 基于Java的朱氏集团客户关系管理系统设计源码
- 基于C++的作业提交与批改系统设计源码
- 基于Vue2的移动端电影资讯网站设计源码
- 高分课程设计作业-基于QT的模仿宝石迷阵游戏C++源码+文档说明+界面截图
- 基于Apache Spark的Spark DistCP重实现设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页