Cocos2d-x示例:单点触摸事件
为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例。使用事件触发器实现单点触摸事件。该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们。事件响应优先级是依照它们的显示顺序。
为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例。使用事件触发器实现单点触摸事件。该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们。事件响应优先级是依照它们的显示顺序。
以下我们再看看详细的程序代码,首先看一下HelloWorldScene.h文件,它的代码例如以下:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
typedef enum ①
{
kBoxA_Tag = 102
,kBoxB_Tag
,kBoxC_Tag
} SpriteTags; ② class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtualvoid onEnter(); ③
virtualvoid onExit(); ④ booltouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); ⑤
void touchMoved(cocos2d::Touch *touch, cocos2d::Event *event); ⑥
void touchEnded(cocos2d::Touch *touch, cocos2d::Event *event); ⑦ CREATE_FUNC(HelloWorld);
}; #endif // __HELLOWORLD_SCENE_H__
上述代码第①~②行是定义个枚举类型SpriteTags。枚举类型SpriteTags中定义了三个常量。这三个常量相应场景中的三个精灵的标签(Tag)属性。代码第③行声明了层声明周期的onEnter()函数。我们将在该函数中注冊监听器和初始化设置。第④行代码是声明了层声明周期的onExit()函数。我们将在该函数中注销监听器和释放一些资源。代码第⑤~⑥行是声明单点触摸事件回调函数。
HelloWorldScene的实现代码HelloWorldScene.ccp文件,它的HelloWorld::init()代码例如以下:
bool HelloWorld::init()
{
if( !Layer::init() )
{
returnfalse;
} SizevisibleSize = Director::getInstance()->getVisibleSize();
Pointorigin = Director::getInstance()->getVisibleOrigin(); //贴图的纹理图片宽高必须是2的n次幂,128x128
autobg = Sprite::create("BackgroundTile.png",
Rect(0,0, visibleSize.width, visibleSize.height)); ①
//贴图的纹理參数。水平反复平铺。垂直反复平铺
Texture2D::TexParamstp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; ②
bg->getTexture()->setTexParameters(tp); ③
bg->setPosition(origin+ Point(visibleSize.width/2, visibleSize.height/2));
addChild(bg,0); ④ Sprite*boxA = Sprite::create("BoxA2.png"); ⑤
boxA->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2) + Point(-120, 120));
addChild(boxA,10, kBoxA_Tag); Sprite*boxB = Sprite::create("BoxB2.png");
boxB->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2));
addChild(boxB,20, kBoxB_Tag); Sprite*boxC = Sprite::create("BoxC2.png");
boxC->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2) + Point(120, 160));
addChild(boxC,30, kBoxC_Tag); ⑥ returntrue;
}
我们在HelloWorld::init()函数中初始化了场景中的背景和三个方块精灵。代码第①~④行是创建并加入背景,图8-3所看到的的背景是由一个128x128纹理图片(BackgroundTile.png)重复贴图上,这样能够降低内存消耗,在第①行代码中创建背景精灵对象。注意背景的大小仍然是整个屏幕。第②行代码是设置贴图的纹理的參数。Texture2D::TexParams类型是一个结构体。
第③行代码是将參数设置到背景精灵的纹理上。第④行代码是加入背景精灵到当前层。
代码第⑤~⑥行是创建了三个方块精灵,在加入它到当前层的时候我们使用三个參数的addChild(Node* child,int localZOrder,int tag)函数,这样能够通过localZOrder參数指定精灵的显示顺序。
HelloWorldScene.ccp中的HelloWorld::onEnter()代码例如以下:
void HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorldonEnter"); autolistener = EventListenerTouchOneByOne::create(); ① listener->setSwallowTouches(true); ②
listener->onTouchBegan= CC_CALLBACK_2(HelloWorld::touchBegan, this); ③
listener->onTouchMoved= CC_CALLBACK_2(HelloWorld::touchMoved,this); ④
listener->onTouchEnded= CC_CALLBACK_2(HelloWorld::touchEnded,this); ⑤ //加入监听器
EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher(); ⑥
eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
getChildByTag(kBoxA_Tag)); ⑦
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
getChildByTag(kBoxB_Tag)); ⑧
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
getChildByTag(kBoxC_Tag)); ⑨ }
上述代码第①行是创建一个单点触摸事件监听器对象。第②行代码是设置是否吞没事件,假设设置为true,那么在onTouchBegan函数返回 true 时吞没事件,事件不会传递给下一个Node对象。第③行代码是设置监听器的onTouchBegan属性回调函数。第④行代码是设置监听器的onTouchMoved属性回调函数。
第⑤行代码是设置监听器的onTouchEnded属性回调函数。
代码第⑥~⑨行是加入监听器。当中第⑦行使用精灵显示优先级加入事件监听器。当中參数getChildByTag(kBoxA_Tag)是通过精灵标签Tag实现获得精灵对象。第⑧行和第⑨行代码是为另外两精灵加入事件监听器。当中listener->clone()获得listener对象。使用clone()函数是由于每个事件监听器仅仅能被加入一次。addEventListenerWithSceneGraphPriority和addEventListenerWithFixedPriority会在加入事件监听器时设置一个注冊标识。一旦设置了注冊标识。该监听器就不能再用于注冊其他事件监听了。因此我们须要使用listener->clone()克隆一个新的监听器对象,把这个新的监听器对象用于注冊。
HelloWorldScene.ccp中的触摸事件回调函数代码例如以下:
bool HelloWorld::touchBegan(Touch*touch, Event* event) ①
{
//获取事件所绑定的 target
autotarget = static_cast<Sprite*>(event->getCurrentTarget()); ②
PointlocationInNode = target->convertToNodeSpace(touch->getLocation()); ③
Sizes = target->getContentSize(); ④
Rectrect = Rect(0, 0, s.width, s.height); ⑤ //点击范围推断检測
if(rect.containsPoint(locationInNode)) ⑥
{
log("spritex = %f, y = %f ", locationInNode.x, locationInNode.y);
log("spritetag = %d", target->getTag());
target->runAction(ScaleBy::create(0.06f,1.06f)); ⑦
returntrue; ⑧
}
returnfalse;
} void HelloWorld::touchMoved(Touch*touch, Event *event) ⑨
{
log("onTouchMoved");
autotarget = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition()+ touch->getDelta()); ⑩
} void HelloWorld::touchEnded(Touch*touch, Event *event) ⑪
{
log("onTouchEnded");
autotarget = static_cast<Sprite*>(event->getCurrentTarget());
log("spriteonTouchesEnded.. "); PointlocationInNode = target->convertToNodeSpace(touch->getLocation());
Sizes = target->getContentSize();
Rectrect = Rect(0, 0, s.width, s.height);
//点击范围推断检測
if(rect.containsPoint(locationInNode))
{
log("spritex = %f, y = %f ", locationInNode.x, locationInNode.y);
log("spritetag = %d", target->getTag());
target->runAction(ScaleTo::create(0.06f,1.0f));
}
}
上代码第①行是定义回调函数touchBegan。
第②行代码是获取事件所绑定的精灵对象,当中event->getCurrentTarget()语句返回值是Node对象。static_cast<Sprite*>是强制类型转换为Sprite对象。
第③行代码是获取当前触摸点相对于target对象的本地坐标。第④行代码是获得target对象的尺寸。第⑤行代码是通过target对象的尺寸创建Rect变量。
第⑥行代码rect.containsPoint(locationInNode)是推断是否触摸点在target对象范围。
第⑦行代码是放大target对象。
第⑧行代码返回true,表示能够回调第⑨行touchMoved函数和第⑪行touchEnded函数。第⑩行代码是移动target对象的位置。
HelloWorldScene.ccp中的HelloWorld::onExit()代码例如以下:
void HelloWorld::onExit()
{
Layer::onExit();
log("HelloWorldonExit");
Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}
上述HelloWorld::onExit()函数是退出层时候回调,我们在这个函数中注销全部的监听事件。
提示 多点触摸事件是与详细的平台有关系的,在Win32平台下我们无法測试多点触摸。其实多点触摸和单点触摸开发流程基本类似,这里我们就不再赘述了。
京东:http://item.jd.com/11584534.html
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源代码及样章下载地址:
样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1
版权声明:本文博主原创文章。博客,未经同意不得转载。
Cocos2d-x示例:单点触摸事件的更多相关文章
- Cocos2d-x开发实例:单点触摸事件
下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程.感受一下它的缺点和优点.该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们. 下面我们看看HelloWorldScen ...
- Cocos2d-x实例:单点触摸事件
addChild(boxC,30, kBoxC_Tag); ...
- registerWithTouchDispatcher 注册单点触摸事件
Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...
- 移动端-js触摸事件
开发者工具 在移动开发中,一种较为容易的做法是,先在桌面上开始原型设计,然后再在打算要支持的设备上处理移动特有的部分.多点触摸正是难以在PC上进行测试的那些功能之一,因为大部分的PC都没有触摸输入. ...
- javascript触摸事件touch使用
详细内容请点击 Apple在iOS 2.0中引入了触摸事件API,Android正迎头赶上这一事实标准,缩小差距.最近一个W3C工作组正合力制定这一触摸事件规范. 在本文深入研究iOS和 ...
- iOS开发之触摸事件及手势
1.iOS中的事件 在用户使用app过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型: 2.响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并 ...
- 【转载】Quick 中的触摸事件
原文地址 http://cn.cocos2d-x.org/article/index?type=quick_doc&url=/doc/cocos-docs-master/manual/fram ...
- Cocos2d-x中触摸事件
理解一个触摸事件可以从时间和空间两方面考虑. 1.触摸事件的时间方面 触摸事件的在时间方面,如下图所示,可以有不同的“按下”.“移动”和“抬起”等阶段,表示触摸是否刚刚开始.是否正在移动或处于静止状态 ...
- Android触摸事件(一)-TouchEventHelper
文件夹 文件夹 概述 关于更新 2016-08-31 2016-06-20 关于单点触摸事件singleTouch 单击的两种方式 关于双击事件 双击事件的检測逻辑 双击事件触发的时机 关于多点触摸事 ...
随机推荐
- 获取CentOS软件源中的updates包
之前在本地网络中建了一个CentOS软件源,挺好用的,可是发现有些软件还是装不上,因为系统安装盘中的包并不全,有些软件的依赖在updates源中,updates源在网上,怎么把其中的包拿到呢?一种方法 ...
- VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器
VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器 View 传输服务器用于管理和简化数据中心与在最终用户本地系统上检出使用的 View 桌面之间的数据传输.必须安 ...
- ruby中的模块
什么是模块 模块(module)是Ruby特有的功能之一.类用来表现具有数据与行为(程序)的"东西", 而模块大致来说,则是只有程序部分的集合体.类与模块最大的不同在于: 1.模块 ...
- hdu 5071 Chat(模拟)
题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...
- Visual Studio使用正则表达式快速统计总共代码行数
原文:Visual Studio使用正则表达式快速统计总共代码行数 按CTRL+SHIFT+F,勾上支持正则表达式,然后输入搜索内容: <span style="font-family ...
- AOP 之 6.1 AOP基础 ——跟我学spring3(转)
http://jinnianshilongnian.iteye.com/blog/1418596
- 获取不同机型外置SD卡路径
/** * 运行挂载命令.返回挂载的地址.然后将地址解析 */ private void getExtSDCardPath() { try { Runtime runtime = Runtime.ge ...
- Jndi使用好处,与简单实例【Tomcat】
JNDI学习总结(一)——JNDI数据源的配置 一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动 ...
- 阅读zepto.js的core中的Core methods
学习zepto.js,參考资料:http://www.zeptojs.cn/ 跟jQuery一样.其选择符号也是$; 首先接触的是 $.() 选择 $(selector, [context]) ⇒ ...
- Context Switch and System Call
How many Context Switches is “normal”? This depends very much on the type of application you run. If ...