加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-js04_游戏触摸交互的监听和实现

发布时间:2020-12-14 21:36:43 所属栏目:百科 来源:网络整理
导读:Cocos2d-js04_游戏触摸交互的监听和实现 1、定义触摸函数,注册监听对象及回调方法,代码: //创建监听对象,回调方法 cc .eventManager. addListener ({ event : cc . EventListener . TOUCH_ONE_BY_ONE , swallowTouches : false , onTouchBegan : this .

Cocos2d-js04_游戏触摸交互的监听和实现

1、定义触摸函数,注册监听对象及回调方法,代码:

//创建监听对象,回调方法

cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:false,
onTouchBegan:this.onTouchBegan,
onTouchMoved:this.onTouchMoved,
onTouchEnded:this.onTouchEnded
},this);

3、实现触摸机制,代码:

 onTouchBegan:function(touch,event){
 //cc.log("你点击了当前的屏幕!");  var target = event.getCurrentTarget();
 var mx = Math.abs((touch.getLocation().x - 5) - target._head.now_col*63);
 var my = Math.abs((touch.getLocation().y - 300) - target._head.now_row*63);
 if(mx > my){//左右移动  if((touch.getLocation().x - 5) > target._head.now_col*63){
 //向右移动  dir = SNAKE_DIR.RIGHT;
 }else{
 //向左移动  dir = SNAKE_DIR.LEFT;
 }

 }else{//上下移动  if((touch.getLocation().y - 300) > target._head.now_row*63){
 //向上移动  dir = SNAKE_DIR.UP;
 }else{
 //向下移动  dir = SNAKE_DIR.DOWN;
 }

 }
},
 
 
视频地址:http://www.9miaoketang.com/course/37
课程讨论帖地址:http://www.9miao.com/thread-64587-1-1.html
源码地址:https://store.cocos.com/stuff/show/128289.html
QQ交流群:83459374
后期也会把该源码传在群里面去,欢迎大家加入讨论!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读