Laya2 API:官方文档
Laya.Handler回调传参数:
传一个参数:Laya.Handler.create(this, callback,[1])
传多个参数:Laya.Handler.create(this, callback,[[0,1]])
多点触控
安卓:e.touchId 第一个是0,第二个是1,固定值
IOS: e.touchId每次再按下时,都会是一个新的数字,拖动时保持同一个数字
需要自己特殊处理下
private OnMouseDown(e:Laya.Event){
console.log("OnMouseDown touchId:",e.touchId);
if(this.touch1==-1){
this.touch1 = e.touchId;
console.log("设置touch1:",this.touch1);
}else{
if(this.touch2==-1){
this.touch2 = e.touchId;
console.log("设置touch2:",this.touch2);
}
}
if(e.touchId == this.touch1){
this.ui.on(Laya.Event.MOUSE_MOVE, this, this.OnMouseMove);
this.ui.on(Laya.Event.MOUSE_UP, this, this.OnMouseUp);
}
if(e.touchId == this.touch2){
}
}
//
private OnMouseMove(e:Laya.Event){
//console.log("OnMouseMove touchId:",e.touchId)
if(e.touchId == this.touch1){
}
if(e.touchId == this.touch2){
}
}
private OnMouseUp(e:Laya.Event){
console.log("OnMouseUp touchId:",e.touchId);
if(e.touchId == this.touch1){
this.touch1 = -1;
}
if(e.touchId == this.touch2){
this.touch2 = -1;
}
}
点击区域
1.规则图片,可以通过画图:比如矩形,菱形,圆形等,设置点击区域HitArea。
点击优先级:HitArea > 宽高
let sprite:Laya.Sprite = new Laya.Sprite();
//比如菱形或圆形等,其他图形时,HitArea 特别有用
let hitArea:Laya.HitArea = new Laya.HitArea();
hitArea.hit.drawRect(0,0,50,100,null);
sprite.hitArea = hitArea;
//穿透mouseThrough为true时,只只针对宽高值范围内触发
sprite.width = 100;
sprite.height=100;
sprite.mouseThrough = true;
2.不规则图片,通过获取图片点击像素的透明度来判断。
let img:Laya.Image = new Laya.Image();
//覆写点击区域判断
img.hitTestPoint = (x, y)=>{
var imageData = this.img.source.getPixels(x, y, 1, 1);
//点击像素alpha通道是0,表示点击透明区域
return imageData[3] > 0;
};
//点击事件
private MOUSE_DOWN(evt:Laya.Event):void{
let pos:Laya.Point=new Laya.Point(evt.stageX,evt.stageY);
img.globalToLocal(pos);
if(!img.hitTestPoint(pos.x,pos.y)){
return;
}
}