LayaAir 事件处理器 Handler

目录

Handler 

Handler.create 与 new Handler

编码示例


Handler 

Package laya.utils
public class Handler
Inheritance Handler Inheritance Object

1、Handler 事件处理器类。这是优化内存的方式之一,可以参考《LayaAir 对象池 laya.utils.Pool

2、推荐使用 Handler.create() 方法从对象池创建,减少对象创建消耗。创建的 Handler 对象不再使用后,可以使用 Handler.recover() 将其回收到对象池,回收后不要再使用此对象,否则会导致不可预料的错误。

3、注意:由于鼠标事件也用本对象池,不正确的回收及调用,可能会影响鼠标事件的执行。

4、开发过程中会经常使用 Handler 来完成异步回调。Laya.Handler.create 使用了内置对象池管理,因此在使用Handler对象时可使用 Laya.Handler.create 来创建回调处理器。以下代码使用 Laya.Handler.create 创建资源加载的回调处理器:

Laya.loader.load(urls, Laya.Handler.create(this, onAssetLoaded));

5、游戏中经常根据游戏逻辑和阶段分批加载资源,第一批资源加载完成,触发Laya.Handler.create()创建的complete事件回调方法后被对象池回收;当游戏进行到某个时候,需要加载第二批资源时,Laya.Handler.create()会首先在对象池中检索相同的回调方法处理器,如果找到就直接使用对象池中方法,从而节省了内存开销。

Method(方法)

Handler(caller:* = null, method:Function = null, args:Array = null, once:Boolean = false) ,根据指定的属性值,创建一个 Handler 类的实例。

clear():Handler ,清理对象引用。

create(caller:*, method:Function, args:Array = null, once:Boolean = true):Handler

[static] 从对象池内创建一个Handler,默认会执行一次并立即回收,如果不需要自动回收,设置once参数为false。

recover():void ,清理并回收到 Handler 对象池内。

更多API参见官网:https://layaair.ldc.layabox.com/api/?category=Core&class=laya.utils.Handler

Handler.create 与 new Handler

/**
 * 从对象池内创建一个Handler,默认会执行一次并立即回收,如果不需要自动回收,设置once参数为false。
 * @param   caller 执行域(this)。
 * @param   method 回调方法。
 * @param   args 携带的参数。
 * @param   once 是否只执行一次,如果为true,回调后执行recover()进行回收,默认为true。
 * @return  返回创建的handler实例。
 */
static create(caller: any, method: Function, args?: Array<any>, once?: boolean): Handler;

1、如果需要多次触发Handler返回的回调方法,那么就需要对 Laya.Hanlder.create() 方法中的 once 参数设置为false。或者用 new Laya.Handler() 的方式创建。

2、例如在游戏开始界面中加载资源,对加载资源的进度进行显示时:

Laya.loader.load(urls, Laya.Handler.create(this,onAssetLoaded), Laya.Handler.create(this, onLoading));//错误写法
Laya.loader.load(urls, Laya.Handler.create(this,onAssetLoaded), Laya.Handler.create(this,onLoading, null, false));//正确写法
Laya.loader.load(urls, Laya.Handler.create(this,onAssetLoaded), new Laya.Handler(this, onLoading));//正确写法

3、使用 Laya.Handler.create(this,onLoading) 返回的回调方法,是要处理progress加载进度事件,由于回调执行一次之后就被对象池回收了,所以progress加载进度事件只触发了一次就结束了,但实际上资源并未加载完成,还处于加载中,所以必须设置 once 参数设置为false,或者用 new Laya.Handler() 的方式创建。

/**
 * 根据指定的属性值,创建一个 <code>Handler</code> 类的实例。
 * @param   caller 执行域。
 * @param   method 处理函数。
 * @param   args 函数参数。
 * @param   once 是否只执行一次。
 */

Handler(caller?: any, method?: Function, args?: Array<any>, once?: boolean);

4、注意:new Handler() 是没有使用对象池的方式,Handler.create() 默认使用了对象池.。

编码示例

//初始化引擎
Laya.init(1000, 500, Laya.WebGL);
/**显示性能统计信息 */
Laya.Stat.show(0, 0);
var label_info;

const imgUrls = [];
for (var i = 0; i < 10; i++) {
	imgUrls.push("http://tomcat.apache.org/res/images/tomcat.png");
	imgUrls.push("http://www.apache.org/img/asf_logo.png");
	imgUrls.push("https://cn.vuejs.org/images/logo.png?_sw-precache=cf23526f451784ff137f161b8fe18d5a");
	imgUrls.push("http://img02.tuke88.com/preview_music/00/08/56/preview-5b835eaf13a9b9053.mp3");
	imgUrls.push("https://spring.io/img/homepage/icon-your-app.svg");
	imgUrls.push("http://www.apache.org/images/SupportApache-small.png");
	imgUrls.push("https://avatar.csdn.net/6/D/4/3_wangmx1993328.jpg");
	imgUrls.push("https://official.layabox.com/public/img/LAYABOX_Logo.png");
}

/**load 加载应用中将使用到的资源列表
 * 第1个参表示要加载的单个资源地址或资源信息数组,比如:简单数组:["a.png","b.png"]
 * 第2个参数加载结束回调
 * 第3个参数是加载进度回调,回调参数值为当前资源的加载进度信息(0-1),Handler 要反复回调,所以要设置为 false,否则执行一次就会自动删除的
 */
Laya.loader.retryNum = 2;//加载出错后的重试次数,默认重试一次
Laya.loader.load(imgUrls,
	Laya.Handler.create(this, loadComplete),
	Laya.Handler.create(this, loadProgress, [], false));
showInfo();

/** 资源加载完成时回调*/
function loadComplete(isSuccess) {
	console.log("资源加载完成...", isSuccess);
	label_info.removeSelf();/**加载完成后从父容器中删除标签 label_info */
	var sprite = new Laya.Sprite();
	sprite.graphics.drawTexture(Laya.loader.getRes(imgUrls[0]), 200);
	Laya.stage.addChild(sprite);
}

/**资源加载过程中的进度回调
 * progress 取值 0-1 */
function loadProgress(progress) {
	label_info.text = parseInt(progress * 100) + "%";
	console.log("当前加载资源:" + progress);
}

/**显示标签信息 */
function showInfo() {
	label_info = new Laya.Label("0%");
	label_info.fontSize = 20;
	label_info.color = "#fff";
	label_info.pos(200, 50);
	Laya.stage.addChild(label_info);
}

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/86152443