美文网首页
【转】cocos2djs触摸事件

【转】cocos2djs触摸事件

作者: 紫荆逸 | 来源:发表于2017-06-02 23:41 被阅读45次

    cc.sys.isMobile 判断是不是触屏设备

    'mouse' in cc.sys.capabilities 判断支不支持鼠标事件

    'touches' in cc.sys.capabilities

    'keyboard' in cc.sys.capabilities

    触摸事件--单指触摸:

    if (cc.sys.isMobile) {

    var listener = cc.EventListener.create({

    event:cc.EventListener.TOUCH_ONE_BY_ONE,

    swallowTouches:true,

    onTouchBegan:function(touch,event){

    var target = event.getCurrentTarget();

    //var delta = touch.getLocation();

    //cc.log("x="+delta.x + "" + "y=" + delta.y);

    target.onTouchBegainCallBack();

    target.onTouchMovedCallBack();

    return true;

    },

    onTouchMoved:function(touch,event){

    var target = event.getCurrentTarget();

    //var delta = touch.getLocation();

    //cc.log("x="+delta.x + "" + "y=" + delta.y);

    },

    onTouchEnded:function(touch,event){

    var target = event.getCurrentTarget();

    target.onTouchEndCallBack(event);

    //var delta = touch.getLocation();

    //cc.log("x="+delta.x + "" + "y=" + delta.y);

    }

    });

    cc.eventManager.addListener(listener, this);

    }

    鼠标事件:

    if ('mouse' in cc.sys.capabilities) {

    cc.eventManager.addListener({

    event:cc.EventListener.MOUSE,

    onMouseDown:function(event){

    //cc.log("onMouseDown");

    var pos = event.getLocation();

    if(event.getButton() === cc.EventMouse.BUTTON_RIGHT){

    cc.log("onRightMouseDown at:" + pos.x +" " + pos.y);

    }else if(event.getButton() === cc.EventMouse.BUTTON_LEFT){

    cc.log("onLeftMouseDown at:" + pos.x +" " + pos.y);

    var target = event.getCurrentTarget();

    target.onTouchBegainCallBack();

    target.onTouchMovedCallBack();

    }

    },

    onMouseMove:function(event){

    //cc.log("onMouseMove");

    },

    onMouseUp:function(event){

    var target = event.getCurrentTarget();

    target.onTouchEndCallBack(event);

    //cc.log("onMouseUp");

    }

    }, this);

    键盘事件:

    listener = cc.EventListener.create({

    event:cc.EventListener.KEYBOARD,

    onKeyPressed:function(keyCode,event){

    if (keyCode === 38 || keyCode === 87) {

    var target = event.getCurrentTarget();

    target.onTouchBegainCallBack();

    target.onTouchMovedCallBack();

    }

    },

    onKeyReleased:function(keyCode,event){

    }

    });

    cc.eventManager.addListener(listener, this);

    相关文章

      网友评论

          本文标题:【转】cocos2djs触摸事件

          本文链接:https://www.haomeiwen.com/subject/ehlpfxtx.html