Cocos2d-js04_游戏触摸交互的监听和实现
//创建监听对象,回调方法
cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches:@H_502_32@false,
onTouchBegan:@H_502_32@this.onTouchBegan,
onTouchMoved:@H_502_32@this.onTouchMoved,
onTouchEnded:@H_502_32@this.onTouchEnded
},@H_502_32@this);
3、实现触摸机制,代码:
onTouchBegan:@H_502_32@function(touch,event){ //cc.log("你点击了当前的屏幕!"); @H_502_32@var target = event.getCurrentTarget(); @H_502_32@var mx = Math.abs((touch.getLocation().x - 5) - target._head.now_col*63); @H_502_32@var my = Math.abs((touch.getLocation().y - 300) - target._head.now_row*63); @H_502_32@if(mx > my){//左右移动 @H_502_32@if((touch.getLocation().x - 5) > target._head.now_col*63){ //向右移动 dir = SNAKE_DIR.RIGHT; }@H_502_32@else{ //向左移动 dir = SNAKE_DIR.LEFT; } }@H_502_32@else{//上下移动 @H_502_32@if((touch.getLocation().y - 300) > target._head.now_row*63){ //向上移动 dir = SNAKE_DIR.UP; }@H_502_32@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 后期也会把该源码传在群里面去,欢迎大家加入讨论!