cocos2d-x自定义可点击/可处理事件/可接受触屏消息的Sprite
来源:互联网 发布:怎样在淘宝开店铺介绍 编辑:程序博客网 时间:2024/05/17 23:31
本篇大部分摘自http://blog.csdn.net/onerain88/article/details/7550009(写的好清楚)
一般经常用到的触屏的情况有两种:一种是Layer统一接收触屏消息,然后由程序根据需要分发给不同位置的精灵;另一种情况是自定义一个可以接收触屏消息的Sprite,比如类似于Button功能的Sprite,这就需要在定义Sprite的时候就要定义好触屏所触发的操作!
1.Layer接收触屏消息(请参考原文)
2.自定义可以接收触屏消息的Sprite
首先要先继承CCSprite或者其父类,以满足精灵形状,位置等信息的要求,另外还需要继承触屏事件委托CCTargetedTouchDelegate,CCTargetedTouchDelegate中定义了接收触屏信息的回调虚函数,而这些虚函数,正是我们需要覆写的部分,代码如下
class TouchableSprite: public CCSprite, public CCTargetedTouchDelegate { public: TouchableSprite(); virtual ~TouchableSprite(); static TouchableSprite *touchSpriteWithFile(const char *file); bool initWithFile(const char *file); virtual void onEnter(); virtual void onExit(); CCRect rect(); bool containsTouchLocation(CCTouch *touch); virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event); virtual void ccTouchMoved(CCTouch *touch, CCEvent *event); virtual void ccTouchEnded(CCTouch *touch, CCEvent *event); };
重点在于判断自定义Sprite是否被点击,这时就需要得到精灵所在的矩形了,这时又有两种判断方式
(1)得到触屏所在位置,然后根据精灵所在位置的矩形区域和触屏的点判断是否包含,如果包含,则说明触摸到了Sprite。这里写了一个得到精灵当前所在矩形的方法
CCRect TouchableSprite::rect() { CCSize size = getContentSize(); CCPoint pos = getPosition(); return CCRectMake(pos.x - size.width / 2, pos.y - size.height / 2, size.width, size.height); }然后在每次点击的时候都需要将当前触屏位置转换为GL坐标的位置,然后和Sprite所在矩形做包含判断
bool TouchableSprite::containsTouchLocation(cocos2d::CCTouch *touch) { CCPoint touchPoint = touch->locationInView(touch->view()); //这里由于版本更新,改为touch->getLoactionInView() touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); return CCRect::CCRectContainsPoint(rect(), touchPoint); }转载作者补充:
可以使用getLocation()取代上面那两句代码,cocos2d-x相关源码如下:
// returns the current touch location in screen coordinatesCCPoint CCTouch::getLocationInView() const { return m_point; }// returns the current touch location in OpenGL coordinatesCCPoint CCTouch::getLocation() const{ return CCDirector::sharedDirector()->convertToGL(m_point); }这里要提下坐标系统,getLocationInView获取的是屏幕坐标,不同设备坐标系统不同,例如ios是左上角为原点。而cocos2d-x是左下角为原点。所以在cocos2d-x中做运算时候,要转为GL坐标系,即cocos2d-x默认的“可视屏幕的坐标系统”。
(2)其实cocos2d为我们提供了一种相对简单的方法,但是原理类似,调用CCNode中定义的convertTouchToNodeSpaceAR()方法,将触屏点转化为相对于结点的相对坐标
CCRect TouchableSprite::rect() { CCSize size = getTexture()->getContentSize(); return CCRectMake(-size.width / 2, -size.height / 2, size.width, size.height); }bool TouchableSprite::containsTouchLocation(cocos2d::CCTouch *touch) { return CCRect::CCRectContainsPoint(rect(), convertTouchToNodeSpaceAR(touch)); }
关于事件处理还可以参考本文
http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html
可以借鉴该文点击事件处理方法
void MyLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { // 单点 CCTouch *pTouch = (CCTouch*)(pTouches->anyObject()); // 所有点 for(CCSetIterator iterTouch = pTouches->begin(); iterTouch != pTouches->end(); iterTouch++) { CCTouch *pCurTouch = (CCTouch*)(*iterTouch); } // 获取点在视图中的坐标(左上角为原点) CCPoint touchLocation = pTouch->getLocationInView(); // 把点的坐标转换成OpenGL坐标(左下角为原点) touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); // 把OpenGL的坐标转换成CCLayer的坐标 CCPoint local = convertToNodeSpace(touchLocation) // 大小为100x100,坐标为(0, 0)的矩形 CCRect * rect = CCRectMake(0, 0, 100, 100); // 判断该坐标是否在rect矩形内 bool flag = rect.containsPoint(local) if(flag) { // 回调 } else { // 不执行 }}
其它处理方法,可以借鉴CCTableView
http://blog.csdn.net/xzongyuan/article/details/9177421
触屏传递顺序
另外需要主要的是
virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event);
方法,其返回值是对此触屏消息有影响的,简单来说,如果返回false,表示不处理ccTouchMoved(),ccTouchEnded(),ccTouchCanceld()方法,而交由后续接收触屏消息的对象处理;如果返回true,表示会处理ccTouchMoved(),ccTouchEnded(),ccTouchCanceld()方法,并且消耗掉此触屏消息。
最后,要在进入时候添加触屏接口
void TouchableSprite::onEnter(){CCSprite::onEnter();CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);}void TouchableSprite::onExit(){CCSprite::onExit();CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);}
- cocos2d-x自定义可点击/可处理事件/可接受触屏消息的Sprite
- Cocos2d-x 自定义可接收处理触摸消息精灵类
- Cocos2d-x 可响应触碰事件的 CCSprite
- cocos2d-x处理点击事件的机制
- 自定义可滑动、可点击的开关
- cocos2d-x判断sprite点击
- cocos2d-x判断sprite点击
- 实现自定义android柱状图控件!可显示气温!可相应点击事件的回调!
- cocos2d js v3.10 sprite点击事件的区域问题
- 【Cocos2d-x】可擦除的Layer:ErasableLayer
- cocos2d-x 有关CCEditBox设置除输入框以外的可点击触发区域
- cocos2d-x 自定义Touch事件触屏:
- ListView里面添加Button之类可点击事件Item不可点击的处理方法
- 点击可调用btnAdd的服务器事件
- 可滑动可点击,不占全屏的自定义控件
- Android自动识别<a>标签的自定义TextView(可自定义点击事件)
- cocos2d-x遇到的触屏处理事件问题
- cocos2d-x 自定义Touch事件处理类
- SQL数据库中"简单的"SELECT TOP—可能有你从未注意到的细节
- JQuery 节点遍历
- 安卓开发经验分享:资源、UI、函数库、测试、构建一个都不能少
- Bootloader与Kernel间参数传递机制 taglist
- 为何C语言(的函数调用)需要堆栈,而汇编语言却不需要堆栈
- cocos2d-x自定义可点击/可处理事件/可接受触屏消息的Sprite
- DirectShow 在VS2005中环境配置
- 一个简单MySQL触发器例子
- 页面自动跳转
- 启动过程
- Windows SharePoint Services, Microsoft Office SharePoint Server2007 ,Microsoft Office SharePoint2007
- 磁盘写保护
- JS与SEO的关系
- struts.xml处理同个action不同方法