cocos2d-html5学习之三-为sprite添加触摸事件
2014-04-04 10:21
465 查看
http://www.cnblogs.com/jsxh/p/3495158.html
cocos2d-html5学习之三-为sprite添加触摸事件
1. 在onEnter中注册为代理,由于扑克牌会产生重叠,在选择的时候不能让触摸事件传递到被覆盖的牌上,因此不能使用standardTargetedDelegate。
2. 实现其它几个触摸事件,其中onTouchBegan中需要返回true,否则不会调用后面的onTouchEnded方法。
cocos2d-html5学习之三-为sprite添加触摸事件
在斗地主中,使用了cc.Sprite来实现扑克,但是cc.Sprite默认并不能接收触摸事件,需要手动将它注册到事件分配器中。
1. 在onEnter中注册为代理,由于扑克牌会产生重叠,在选择的时候不能让触摸事件传递到被覆盖的牌上,因此不能使用standardTargetedDelegate。
onEnter:function(){ cc.registerTargetedDelegate(0, true, this); this._touchEnabled=true; this._super(); }
2. 实现其它几个触摸事件,其中onTouchBegan中需要返回true,否则不会调用后面的onTouchEnded方法。
onTouchBegan:function(touches,event){ var rect = this.touchRect(); var point = touches.getLocation(); if(cc.rectContainsPoint(this.touchRect(),touches.getLocation())){ this._touchBegan=true; return true; } return false; }
onTouchEnded:function(touches,event){ if(this._touchBegan){ this._touchBegan=false; if(this.active) { this.active = false; this.setPositionY(this.getPositionY() - 30); } else { this.active = true; this.setPositionY(this.getPositionY() + 30); } } }
相关文章推荐
- cocos2d-html5学习之三-为sprite添加触摸事件
- cocos2d-html5学习之三-为sprite添加触摸事件
- cocos2d-x基础学习--添加触摸事件
- cocos2d-x之为label添加事件监听器,单点触摸
- 【Cocos2d-html5游戏引擎学习笔记(5)】Sprite精灵渲染
- Cocos2d-x2.2 lua下面添加Widget时,触摸事件无法触发
- 【学习3】Cocos2d-x 3.x中使用物理引擎动态添加元素(点击事件)
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸添加点
- 学习笔记--cocos2dx-3.0中LabelTTF添加触摸监听事件
- Cocos2d-x学习之---自定义图标(带触摸事件)
- Cocos2d-iphone 为sprite添加双击的事件响应
- Cocos2d-x学习之---自定义图标(带触摸事件)
- 在cocos2d上添加UIView,触摸事件往下传递
- 10.cocos2dx C++为Sprite添加触摸事件监听器
- Cocos2d-x学习笔记(五)CCLayer分析及输入事件处理(触摸、重力传感器、按键)
- cocos2d-x学习笔记(13)触摸事件(多点触摸)
- COCOS2d中添加UIButton不响应触摸事件---iOS开发之最灵异事件之2
- cocos2d-x学习笔记(2)--向游戏中添加角色(sprite)
- cocos2dx C++为Sprite添加触摸事件监听器
- cocos2d-android学习六 --- 添加触屏事件