/**
* Created by Administrator on 2017/4/20.
*/
var EventLayer = cc.Layer.extend({
ctor: function () {
this._super();
this.init();
var winSize = cc.winSize;
/*************TouchOneByOne****************/
if ('touches' in cc.sys.capabilities){
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled: this.onTouchCancelled
},this)
}else{
trace("TOUCH-ONE-BY-ONE test is not supported on desktop");
}
//第二种
var listenr2 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
target:this,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled: this.onTouchCancelled
});
if ('touches' in cc.sys.capabilities) {
cc.eventManager.addListener(listenr2, this);//监听器,节点对象
}else{
trace("TOUCH-ONE-BY-ONE test is not supported on desktop");
}
/*************Touch_ALL_AT_ONCE****************/
//判断当前平台是否支持多点触摸
if('touches' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesBegan:this.onTouchesBegan,
onTouchesMoved: this.onTouchesMoved,
onTouchesEnded: this.onTouchesEnded,
onTouchesCancelled: this.onTouchesCancelled
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持多点触摸
}
/*************KEYBOARD****************/
//判断当前平台是否支持键盘事件
if('keyboard' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.KEYBOARD,
target:this,
onKeyPressed: function (keyCode, event) {
console.log("onKeyPressed");
var keyCodeStr = String.fromCharCode(keyCode);//按键编码转为字符
console.log(keyCodeStr + "(" + keyCode.toString() + ")按下");
},
onKeyReleased: function (keyCode, event) {
console.log("onKeyReleased");
var keyCodeStr = String.fromCharCode(keyCode);//按键编码转为字符
console.log(keyCodeStr + "(" + keyCode.toString() + ")释放");
}
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持键盘事件
}
/*************Mouse****************/
//判断当前平台是否支持鼠标事件
if('mouse' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.MOUSE,
target:this,
onMouseDown: function (event) {//鼠标事件(按下)
console.log("onMouseDown");
var pos = event.getLocation();
var button = event.getButton();
if (button == cc.EventMouse.BUTTON_LEFT){
console.log("左键按下",+pos.x + " " + pos.y);
}else if (button == cc.EventMouse.BUTTON_RIGHT){
console.log("右键按下",+pos.x + " " + pos.y);
}else if (button == cc.EventMouse.BUTTON_MIDDLE){
console.log("中间滚轮键按下");
}
},
onMouseUp: function ( event) {
console.log("onMouseUp");
var pos = event.getLocation();
console.log("鼠标当前位置: ",pos.x,pos.y );
},
onMouseMove:function(event){
console.log("onMouseMove");
var pos = event.getLocation();
console.log("鼠标当前位置: ",pos.x,pos.y );
},
onMouseScroll:function(event){
var pos = cc.p(event.getScrollX(),event.getScrollY());
console.log("鼠标滚轮滚动X: ",pos.x + "y :" + pos.y );
}
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持鼠标事件
}
return true;
},
onTouchBegan:function(touch, event) {
console.log("onTouchBegan");
//获取当前触发事件的对象
var target = event.getCurrentTarget();
//将点击坐标转换为基于当前触发事件对象的本地坐标
var posInNode = target.convertToNodeSpace(target.getLocation());
//获取当前节点大小
var size = target.getContentSize();
//区域设定
var rect = cc.rect(0,0,size.width,size.height);
//判断触摸点是否在节点区域内
if(!(cc.rectContainsPoint(rect,posInNode))){
cc.log("sprite began... x = " + posInNode.x + ", y = " + posInNode.y);
return false;
}
return true;
},
onTouchMoved:function(touch, event) {
console.log("onTouchMoved");
cc.log("onTouchMoved");
var target = event.getCurrentTarget();
var delta = touch.getDelta();
// 移动当前按钮精灵的坐标位置
target.x += delta.x;
target.y += delta.y;
},
onTouchEnded:function(touch, event) {
var target = event.getCurrentTarget();
console.log("onTouchEnded");
},
onTouchCancelled:function(touch, event) {
console.log("onTouchesEnded");
var target = event.getCurrentTarget();
// 获取当前点击点所在相对按钮的位置坐标
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (cc.rectContainsPoint(rect, locationInNode)) {
cc.log("sprite began... x = " + locationInNode.x + ", y = " + locationInNode.y);
}
},
onTouchesBegan:function(touches,event){
var self = event.getCurrentTarget();
//touches[0]表示捕获第一个触摸点,touches为触摸集合
for (var i = 0;i < touches.length;i++){
var touch = touches[i];
var pos = touch.getLocation();
var id = touch.getID();
//获取点击坐标(基于本地坐标)
var locationInNode = self.convertToNodeSpace(touch.getLocation());
//获取当前节点大小
var size = self.getContentSize();
//区域设定
var rect = cc.rect(0,0,size.width,size.height);
//判断触摸点是否在节点区域内
if (!(cc.rectContainsPoint(rect,locationInNode))){
return false;
}
return true;
}
}
}
);
* Created by Administrator on 2017/4/20.
*/
var EventLayer = cc.Layer.extend({
ctor: function () {
this._super();
this.init();
var winSize = cc.winSize;
/*************TouchOneByOne****************/
if ('touches' in cc.sys.capabilities){
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled: this.onTouchCancelled
},this)
}else{
trace("TOUCH-ONE-BY-ONE test is not supported on desktop");
}
//第二种
var listenr2 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
target:this,
onTouchBegan: this.onTouchBegan,
onTouchMoved: this.onTouchMoved,
onTouchEnded: this.onTouchEnded,
onTouchCancelled: this.onTouchCancelled
});
if ('touches' in cc.sys.capabilities) {
cc.eventManager.addListener(listenr2, this);//监听器,节点对象
}else{
trace("TOUCH-ONE-BY-ONE test is not supported on desktop");
}
/*************Touch_ALL_AT_ONCE****************/
//判断当前平台是否支持多点触摸
if('touches' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesBegan:this.onTouchesBegan,
onTouchesMoved: this.onTouchesMoved,
onTouchesEnded: this.onTouchesEnded,
onTouchesCancelled: this.onTouchesCancelled
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持多点触摸
}
/*************KEYBOARD****************/
//判断当前平台是否支持键盘事件
if('keyboard' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.KEYBOARD,
target:this,
onKeyPressed: function (keyCode, event) {
console.log("onKeyPressed");
var keyCodeStr = String.fromCharCode(keyCode);//按键编码转为字符
console.log(keyCodeStr + "(" + keyCode.toString() + ")按下");
},
onKeyReleased: function (keyCode, event) {
console.log("onKeyReleased");
var keyCodeStr = String.fromCharCode(keyCode);//按键编码转为字符
console.log(keyCodeStr + "(" + keyCode.toString() + ")释放");
}
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持键盘事件
}
/*************Mouse****************/
//判断当前平台是否支持鼠标事件
if('mouse' in cc.sys.capabilities){
var listener = cc.EventListener.create({
event : cc.EventListener.MOUSE,
target:this,
onMouseDown: function (event) {//鼠标事件(按下)
console.log("onMouseDown");
var pos = event.getLocation();
var button = event.getButton();
if (button == cc.EventMouse.BUTTON_LEFT){
console.log("左键按下",+pos.x + " " + pos.y);
}else if (button == cc.EventMouse.BUTTON_RIGHT){
console.log("右键按下",+pos.x + " " + pos.y);
}else if (button == cc.EventMouse.BUTTON_MIDDLE){
console.log("中间滚轮键按下");
}
},
onMouseUp: function ( event) {
console.log("onMouseUp");
var pos = event.getLocation();
console.log("鼠标当前位置: ",pos.x,pos.y );
},
onMouseMove:function(event){
console.log("onMouseMove");
var pos = event.getLocation();
console.log("鼠标当前位置: ",pos.x,pos.y );
},
onMouseScroll:function(event){
var pos = cc.p(event.getScrollX(),event.getScrollY());
console.log("鼠标滚轮滚动X: ",pos.x + "y :" + pos.y );
}
});
cc.eventManager.addListener(listener,this);
}else {
//当前平台不支持鼠标事件
}
return true;
},
onTouchBegan:function(touch, event) {
console.log("onTouchBegan");
//获取当前触发事件的对象
var target = event.getCurrentTarget();
//将点击坐标转换为基于当前触发事件对象的本地坐标
var posInNode = target.convertToNodeSpace(target.getLocation());
//获取当前节点大小
var size = target.getContentSize();
//区域设定
var rect = cc.rect(0,0,size.width,size.height);
//判断触摸点是否在节点区域内
if(!(cc.rectContainsPoint(rect,posInNode))){
cc.log("sprite began... x = " + posInNode.x + ", y = " + posInNode.y);
return false;
}
return true;
},
onTouchMoved:function(touch, event) {
console.log("onTouchMoved");
cc.log("onTouchMoved");
var target = event.getCurrentTarget();
var delta = touch.getDelta();
// 移动当前按钮精灵的坐标位置
target.x += delta.x;
target.y += delta.y;
},
onTouchEnded:function(touch, event) {
var target = event.getCurrentTarget();
console.log("onTouchEnded");
},
onTouchCancelled:function(touch, event) {
console.log("onTouchesEnded");
var target = event.getCurrentTarget();
// 获取当前点击点所在相对按钮的位置坐标
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize();
var rect = cc.rect(0, 0, s.width, s.height);
// 点击范围判断检测
if (cc.rectContainsPoint(rect, locationInNode)) {
cc.log("sprite began... x = " + locationInNode.x + ", y = " + locationInNode.y);
}
},
onTouchesBegan:function(touches,event){
var self = event.getCurrentTarget();
//touches[0]表示捕获第一个触摸点,touches为触摸集合
for (var i = 0;i < touches.length;i++){
var touch = touches[i];
var pos = touch.getLocation();
var id = touch.getID();
//获取点击坐标(基于本地坐标)
var locationInNode = self.convertToNodeSpace(touch.getLocation());
//获取当前节点大小
var size = self.getContentSize();
//区域设定
var rect = cc.rect(0,0,size.width,size.height);
//判断触摸点是否在节点区域内
if (!(cc.rectContainsPoint(rect,locationInNode))){
return false;
}
return true;
}
}
}
);