cocos2dx 3.0 触摸机制
在cocos2dx 3.0版本号中,废弃了以往2.x版本号的写法,我们先来看一下Layer.h中的一段代码
/* Callback function should not be deprecated, it will generate lots of warnings.
Since 'setTouchEnabled' was deprecated, it will make warnings if developer overrides onTouchXXX and invokes setTouchEnabled(true) instead of using EventDispatcher::addEventListenerWithXXX.
*/
//单点触摸
virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
virtual void onTouchCancelled(Touch *touch, Event *unused_event);
//多点触摸
virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
单点触摸:(即仅仅有注冊的Layer才干接收触摸事件)
onTouchBegan:假设返回true:本层的兴许Touch事件能够被触发,并阻挡向后层传递
假设返回false,本层的兴许Touch事件不能被触发,并向后传递
简单点来说,假设
1.Layer 仅仅有一层的情况:
单点触摸简单使用方法:
在Layer中加入例如以下代码,重写onTouchxxx函数
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved,this);
listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded,this);
listener->setSwallowTouches(true);//不向下传递触摸
dispatcher->addEventListenerWithSceneGraphPriority(listener,this);
listener->setSwallowTouches(true),不向下触摸,简单点来说,比方有两个sprite ,A 和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响
listener->setSwallowTouches(false)反之,向下传递触摸,触摸A也等于触摸了B
多点触摸点单使用方法(多个Layer获取屏幕事件):
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener1 = EventListenerTouchAllAtOnce::create();
listener1->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan,this);
listener1->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved,this);
listener1->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded,this);
dispatcher->addEventListenerWithSceneGraphPriority(listener1,this);
或者setTouchEnabled(true),然后重写layer的onTouchsxxx函数
关于eventDispatcher:
获取方法:
auto dispatcher = Director::getInstance()->getEventDispatcher();
事件监听器包括下面几种:
- 触摸事件 (EventListenerTouch)
- 键盘响应事件 (EventListenerKeyboard)
- 加速记录事件 (EventListenerAcceleration)
- 鼠标响应事件 (EventListenerMouse)
- 自己定义事件 (EventListenerCustom)
以上事件监听器统一由
_eventDispatcher来进行管理。
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
代码展开一下:
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
{
CCASSERT(listener && node, "Invalid parameters.");
CCASSERT(!listener->isRegistered(), "The listener has been registered."); if (!listener->checkAvailable())
return; listener->setSceneGraphPriority(node);
listener->setFixedPriority(0);
listener->setRegistered(true); addEventListener(listener);
}
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
{
CCASSERT(listener, "Invalid parameters.");
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority."); if (!listener->checkAvailable())
return; listener->setSceneGraphPriority(nullptr);
listener->setFixedPriority(fixedPriority);
listener->setRegistered(true);
listener->setPaused(false); addEventListener(listener);
}
的事件监听器优先级是0,并且在 addEventListenerWithFixedPriority 中的事件监听器的优先级不能够设置为 0,由于这个是保留给 SceneGraphPriority 使用的。
dispatcher->removeEventListener(listener);
cocos2dx 3.0 触摸机制的更多相关文章
- [cocos2d-x 3.0] 触摸显示器
一.基本使用 1.首先,声明一个监听器 有两种,EventListenerTouchOneByOne 和 EventListenerTouchAllAtOnce,前者是单点触控.后者是多点触控.后者我 ...
- cocos2dx 3.2 事件机制
一个sprite的情况 // oneSprite void HelloWorld::touchableSpriteTestOne() { Vec2 origin = Director::getInst ...
- Cocos2d-x 3.0 屏幕触摸及消息分发机制
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)
源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...
- cocos2d-x 3.0 事件分发机制
在cocos2d-x 3.0中一共有五个事件监听器: 触摸事件(EventListenerTouch) 键盘响应事件 (EventListenerKeyboard) 加速器记录事件(EventList ...
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸加入点
/* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...
- cocos2d-x 3.0 内存管理机制
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Cocos2d-x 3.0 事件系统【转】
事件系统,是一个软件的核心组成部分.从小处讲它是应用程序内部各模块交互的设计模式,从大处讲,它是软件架构的组成模块.在现代软件开发中,操作系统通常通过一些预定义的事件,告知应用程序发生的一些事情如用户 ...
- Cocos2dx 3.0 交流篇
创建项目: For(MAC) Runtime Requirements Android 2.3 or newer iOS 5.0 or newer OS X 10.7 or newer Windows ...
随机推荐
- 【JSP】<meta>标签用法
转载自:http://blog.sina.com.cn/s/blog_65c74cce0102v39z.html 非常感谢这位博主,急着用,改日再细细品味重新整理这篇博文. http-equiv M ...
- 【转】cocos2d-x Lua
Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...
- SPFile的使用
转:http://blog.csdn.net/pclzr/article/details/7591741 SPFile对应于SharePoint对象模型中的文件,它的使用方法与SPFolder类大致相 ...
- protobuf-3.0.0-beta-2 windows编译 x64/x86
V3.0.0 beta2以及之后都是CMake 创建VS Solution,project. 因为只能创建x64的项目工程,有时候需要x86的, 只能创建完x64后,自己修改工程配置弄成x86. 创建 ...
- 【转】u盘不显示盘符
转自http://jingyan.baidu.com/article/f3ad7d0fd0793e09c3345b31.html 我的情况: 电脑只有一个c盘,插入u盘,u盘的盘符为d. 弹出u盘,但 ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
- Inxi:获取Linux系统和硬件信息的神器
导读 在这篇文章里,我们将看到如何使用inxi来获取这些详情信息.在论坛技术支持中,它可以作为调试工具,迅速确定用户的系统配置和硬件信息. Inxi是一个可以获取完整的系统和硬件详情信息的命令行工具, ...
- Object类介绍
一.Object类介绍
- mybatis中的变量#与$
ibatis中使用select top #num# * from tableName出现错误.由于初次用ibatis还不知道在它里边拼写SQL语句的一些规则,导致一些自认为很平常的SQL语句,在它这里 ...