cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)
触摸事件
Sprite* sp1 = Sprite::create("Images/t1.png");
sp1->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
addChild(sp1, 10,1);
auto mTouchListener = EventListenerTouchOneByOne::create(); //单点触摸事件
mTouchListener->setSwallowTouches(true); //true向下传递
mTouchListener->onTouchBegan = [](Touch* touch, Event* event){ //匿名方式设置事件
Sprite* target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect normal = Rect(0, 0, s.width, s.height);
if (normal.containsPoint(locationInNode)){
log("x=%f,y=%f", locationInNode.x, locationInNode.y);
target->setOpacity(0x7F); //设置透明度
return true; //向下传递事件
}
return false;
};
//绑定方式设置事件 其onTouchBegan函数的内容和匿名方式内容一样
//listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
mTouchListener->onTouchMoved = [](Touch* touch, Event* event){}; //触摸移动监听
mTouchListener->onTouchEnded = [](Touch* touch, Event* event){}; //触摸监听结束
/*
_eventDispatcher是Node的属性,通过它管理当前节点(场景、层、精灵等)的全部事件的分发。
但它本身是一个单例模式值的引用,在Node的构造函数中,
通过Director::getInstance()->getEventDispatcher(); 获取,有了这个属性,就能方便的处理事件。
*/
//将触摸事件交给事件分发器管理
_eventDispatcher->addEventListenerWithSceneGraphPriority(mTouchListener, sp1);
/*ps:当再次使用 mTouchListener 的时候,须要使用clone()方法创建一个新的克隆。由于在使用addEventListenerWithSceneGraphPriority方法时,会对当前使用的事件监听器加入�一个已注冊的标记,这使得它不可以被加入�多次。另外,有一点很重要mTouchListener是跟Node绑定的,在Node的析构函数中会被移除。
*/
键盘响应事件
auto mKeyboardListener = EventListenerKeyboard::create(); //键盘响应事件 //键盘按下事件监听
mKeyboardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){
log("keyCode=%d", keyCode);
};
//键盘释放事件监听
mKeyboardListener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){
log("keyCode=%d", keyCode);
if (EventKeyboard::KeyCode::KEY_ESCAPE == keyCode){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif
Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
}; _eventDispatcher->addEventListenerWithSceneGraphPriority(mKeyboardListener, this); //将键盘响应事件交给分发器来管理
鼠标响应事件
auto _mouseListener = EventListenerMouse::create(); // 事件响应逻辑
_mouseListener->onMouseMove = [=](Event *event){
EventMouse* e = (EventMouse*)event;
log("Key=%d", e->getMouseButton());
};
_mouseListener->onMouseUp = [=](Event *event){};
_mouseListener->onMouseDown = [=](Event *event){};
_mouseListener->onMouseScroll = [=](Event *event){};
// 加入�到事件分发器
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
自己定义事件
auto mCustomListener = EventListenerCustom::create("my_custom_l", [](EventCustom* event){
char* c = static_cast<char*>(event->getUserData());
log("%s", c);
});
/*
将自自己定义事件交给分发器来管理,这样的方式是使用优先级别来设置
SceneGraphPriority和FixedPriority差别在于前者是在析构函数中会被移除
后者是须要手动移除
*/_eventDispatcher->addEventListenerWithFixedPriority(mCustomListener, 1);
EventCustom _event("my_custom_l");
//char* cstr = "this is my custom listener!";
static int count = 0;
++count;
char* buf;
sprintf(buf, "this is my custom %d", count);
_event.setUserData(buf);
_eventDispatcher->dispatchEvent(&_event); //手动触发自己定义事件
//启动加速硬件设备
Device::setAccelerometerEnabled(true); auto mAcListener = EventListenerAcceleration::create([](Acceleration* acc, Event* event){
#define FIX_POS(_pos, _min, _max) \
if (_pos < _min) \
_pos = _min; \
else if (_pos > _max) \
_pos = _max; \
//log("x=%lf,y=%lf", acc->x, acc->y); auto ballSize = sp1->getContentSize(); auto ptNow = sp1->getPosition(); ptNow.x += acc->x * 9.81f;
ptNow.y += acc->y * 9.81f; FIX_POS(ptNow.x, (VisibleRect::left().x + ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
FIX_POS(ptNow.y, (VisibleRect::bottom().y + ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
sp1->setPosition(ptNow);
}); _eventDispatcher->addEventListenerWithSceneGraphPriority(mAcListener, this);
cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)的更多相关文章
- Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制
EventDispatcher事件分发机制先创建事件,注册到事件管理中心_eventDispatcher,通过发布事件得到响应进行回调,完成事件流. 有五种不同的事件机制:EventListenerT ...
- cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析
(一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...
- Cocos2d-X研究之v3.x 事件分发机制具体解释
事件分发机制 " src="http://www.cgzhw.com/wp-content/uploads/2014/07/inherent3.png" style=&q ...
- Android事件分发机制浅谈(一)
---恢复内容开始--- 一.是什么 我们首先要了解什么是事件分发,通俗的讲就是,当一个触摸事件发生的时候,从一个窗口到一个视图,再到一个视图,直至被消费的过程. 二.做什么 在深入学习android ...
- Android事件分发机制浅谈(二)--源码分析(ViewGroup篇)
上节我们大致了解了事件分发机制的内容,大概流程,这一节来分析下事件分发的源代码. 我们先来分析ViewGroup中dispatchTouchEvent()中的源码 public boolean dis ...
- Atitit View事件分发机制
1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...
- Android Touch事件分发机制学习
Android 事件分发机制 ViewGroup dispatchTouchEvent 返回true dispatchTouchEvent: Activity ACTION_DOWN Myrelat ...
- android 事件分发机制
1.View的事件分发机制 一个button,简单一点就是onTouch,还有onclick事件,我们一个一个来分析 首先响应的是dispatchTouchEvent public boolean d ...
- Android View 事件分发机制 源码解析 (上)
一直想写事件分发机制的文章,不管咋样,也得自己研究下事件分发的源码,写出心得~ 首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个My ...
- Android中View的事件分发机制
简介 事件也称MotionEvent,事件分发机制就是对MotionEvent事件的分发过程,即当一个MotionEvent发生之后,系统需要把这个事件传递给一个具体的View. 点击事件的分发过程由 ...
随机推荐
- sql server日期字段值的比较
sql server中对日期字段值的比较 sql server中对日期字段的比较方式有多种,介绍几种常用的方式:用northwind库中的employees表作为用例表.1.between...and ...
- URL vs. HTML 录制模式
转自:http://blog.csdn.net/testing_is_believing/article/details/5274188 一般来说,如果是标准使用IE访问的B/S架构,应该使用HTML ...
- Object-c @property的用法
property是一种代码生成机制,可以生成不同类型的getter/setter函数,特别是假设你想要用点(.)操作符号来存取变量的话,你就能必须使用property. 怎样使用? 使用方法如:@pr ...
- Swift - 发送消息(文本,图片,文件等)给微信好友或分享到朋友圈
通过调用微信提供的API接口,我们可以很方便的在应用中发送消息给微信好友,或者分享到朋友圈.在微信开发平台(https://open.weixin.qq.com)里,提供了详细的说明文档和样例.但由于 ...
- CSDN头版头条 《近匠》 Wijmo 5 CTO:从Web到移动,我的25年编程生涯
现年52岁的Bernardo Castilho先生是GrapeCity(中文名为葡萄城)ComponentOne公司的CTO,在与他的对话过程中.充满风趣严谨和厚重的历史感. 当作为年轻人的我们崇拜着 ...
- 【app】遍历目录所有文件
遍历目录所有文件 原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...
- Usaco 1.3.2 修理牛棚(Barn Repair)
Barn Repair 题意:在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 剩下的牛一个紧挨着另一个被排成一行来过夜. 有些牛棚里有 ...
- 从response.header中提取cookie,在request里添加cookie
// List<String> resp = new ArrayList<String>(); // HeaderIterator headers ...
- uva 10603
紫皮书的例题 照着敲了一遍,非原创 大题思路主要是三杯水,而水的总数是知道的,相当于知道第一第二杯水的体积,第三杯水的体积也就确定了. 用第一第二杯水的体积来标记数组是否遍历过 优先队列来找移动体积最 ...
- android面试题2
一.属于GLSurFaceView特性的是: 1.管理一个surface,这个surface就是一块特俗的内存.能直接排版到Android的视图view上. 2.管理一个EGL display,它能让 ...