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

来源:互联网 发布:手机报纸软件 编辑:程序博客网 时间:2024/06/05 19:52

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.htmlQQ交流群:83459374后期也会把该源码传在群里面去,欢迎大家加入讨论!

 

0 0