触摸事件

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 学习笔记七)的更多相关文章

  1. Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制

    EventDispatcher事件分发机制先创建事件,注册到事件管理中心_eventDispatcher,通过发布事件得到响应进行回调,完成事件流. 有五种不同的事件机制:EventListenerT ...

  2. cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析

    (一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...

  3. Cocos2d-X研究之v3.x 事件分发机制具体解释

    事件分发机制 " src="http://www.cgzhw.com/wp-content/uploads/2014/07/inherent3.png" style=&q ...

  4. Android事件分发机制浅谈(一)

    ---恢复内容开始--- 一.是什么 我们首先要了解什么是事件分发,通俗的讲就是,当一个触摸事件发生的时候,从一个窗口到一个视图,再到一个视图,直至被消费的过程. 二.做什么 在深入学习android ...

  5. Android事件分发机制浅谈(二)--源码分析(ViewGroup篇)

    上节我们大致了解了事件分发机制的内容,大概流程,这一节来分析下事件分发的源代码. 我们先来分析ViewGroup中dispatchTouchEvent()中的源码 public boolean dis ...

  6. Atitit View事件分发机制

    1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...

  7. Android Touch事件分发机制学习

    Android  事件分发机制 ViewGroup dispatchTouchEvent 返回true dispatchTouchEvent: Activity ACTION_DOWN Myrelat ...

  8. android 事件分发机制

    1.View的事件分发机制 一个button,简单一点就是onTouch,还有onclick事件,我们一个一个来分析 首先响应的是dispatchTouchEvent public boolean d ...

  9. Android View 事件分发机制 源码解析 (上)

    一直想写事件分发机制的文章,不管咋样,也得自己研究下事件分发的源码,写出心得~ 首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个My ...

  10. Android中View的事件分发机制

    简介 事件也称MotionEvent,事件分发机制就是对MotionEvent事件的分发过程,即当一个MotionEvent发生之后,系统需要把这个事件传递给一个具体的View. 点击事件的分发过程由 ...

随机推荐

  1. oracle 分区表exchange原理

    oracle分区的exchange操作非常快,那原理是什么呢?下面我们来做个实验: SQL> create table test (id number(3)); 表已创建. SQL> in ...

  2. 最大似然预计(Maximum likelihood estimation)

    一.定义     最大似然预计是一种依据样本来预计模型參数的方法.其思想是,对于已知的样本,如果它服从某种模型,预计模型中未知的參数,使该模型出现这些样本的概率最大.这样就得到了未知參数的预计值. 二 ...

  3. POJ - 1006 Biorhythms 周期相遇 两个思路程序

    Description Some people believe that there are three cycles in a person's life that start the day he ...

  4. Android中贝塞尔曲线的绘制方法

    贝塞尔曲线,很多人可能不太了解,什么叫做贝塞尔曲线呢?这里先做一下简单介绍:贝塞尔曲线也可以叫做贝济埃曲线或者贝兹曲线,它由线段与节点组成,节点是可拖动的支点,线段像可伸缩的皮筋.一般的矢量图形软件常 ...

  5. HTML+CSS - 搜索 And 高级搜索

  6. asp.net检查验证字符串是否为纯数字方法小结

    原文  asp.net检查验证字符串是否为纯数字方法小结 在asp.net中验证字符串是不是为数字我们没有像php中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...

  7. win 8.1 安装 SQL server 遇到的各种问题

    企业版 SQL Server ed2k://|file|cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso|5054384128| ...

  8. JavaScript常用全局属性与方法

    最近,在学习JavaScript,Java作域链包含全局,记录下常用的全局属性与方法,就当是知识的积累,未列出全部,如需查看全部可参考JS相关的API文档. 常用的全局属性:  全局属性      作 ...

  9. hdu4847 Wow! Such Doge!(简单题+坑爹的输入)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php ...

  10. Oracle安装配置流程

    Oracle安装流程 第一次自己动手安装oracle,之前对oracle安装配置一窍不通,最后最终弄好.总结下. 1.  安装oracle10gserver端 2.  安装oracle10gclien ...