Laya API学习总结

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;
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值