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. 点击事件的分发过程由 ...
随机推荐
- Oracle、DB2、MySql、SQLServer JDBC驱动
四种数据库JDBC驱动,还列出了连接的Class驱动名和Url Pattern,DB2包括Type 2.Type 3和Type 4三种模式.注意驱动包名称的大小写. Oralce连接驱动包名和URL ...
- HTML+CSS+JS - 5秒钟之后跳转页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...
- qt学习笔记(七)之数据库简介(所有支持数据库类型的列表)
笔者最近用Qt写公司的考勤机.本来要求是要基于frameBuffer下用自己开发的easyGUI来进行上层应用开发,但是考虑到easyGUI提供的接口不是很多,就考虑用Qt来开发,顺带练练手. 废话不 ...
- 认识axure组件区域
组件区域也叫做部件区域,英文为widgets,还有人称之为控件区域,组件是axure事先准备好的网站项目常用的零件,比如一些基本的页面元素 Axure默认存在2个组件库,分别为线框图和流程图.同时我们 ...
- 最新 Druid 配置
Druid是一个JDBC组件库,包括数据库连接池.SQL Parser等组件.DruidDataSource是最好的数据库连接池.下面我们就一起来在项目中配置Druid吧 1.Druid依赖配置 &l ...
- perl Exporter一些神奇写法
use base qw(Exporter); @JSON::EXPORT = qw(from_json to_json jsonToObj objToJson encode_json decode_j ...
- sharepoint 2010 在自定义列表的字段上增加功能菜单
sharepoint 2010 在自定义列表的字段上增加功能菜单方法 打开sharepoint designer 2010,找到需要修改的视图页面,例如allitem.aspx,编辑这个页面,点击高级 ...
- [ACM] 九度OJ 1553 时钟
时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1733 解决:656 题目描写叙述: 如图,给定随意时刻,求时针和分针的夹角(劣弧所相应的角). 输入: 输入包括多组測试数据.每组測试数 ...
- Android中获取电池电量
/** * * @author chrp * *显示当前电池电量 */ public class MainActivity extends Activity { private TextView tv ...
- org.apache.hadoop.ipc.Client: Retrying connect to server异常的解决
检查发现是DataNode一直连接不到NameNode. 检查各个节点在etc/hosts中的配置是否有127.0.1.1 xxxxxx.如果有把其屏蔽或者删除,重启各节点即可. 原因:127.0.1 ...