1172人加入学习
(14人评价)
LayaAir2D游戏 - 无限射击

基于LayaAir2.5.0,制作完成于2020年1月31号

价格 ¥ 200.00

1.关于renderHandler所带的默认回调参数

this.characterList.renderHandler = Laya.Handler.create(this,this.listRenderHandler,null,false);

在Handler.creat()方法里,给回调函数传入的参数为null,那么为何能在后面的listRenderHandler(cell,index)函数声明里取得这两个参数?

检查List的源码中渲染Item的函数:

renderItem(cellindex) {
            if (this._array && index >= 0 && index < this._array.length) {
                cell.visible = true;
                if (cell["_$bindData"]) {
                    cell["_dataSource"] = this._array[index];
                    this._bindData(cellthis._array[index]);
                }
                else
                    cell.dataSource = this._array[index];
                if (!this.cacheContent) {
                    this.posCell(cellindex);
                }
                if (this.hasListener(Laya.Event.RENDER))
                    this.event(Laya.Event.RENDER, [cellindex]);
                if (this.renderHandler)
                    this.renderHandler.runWith([cell, index]);
            }

 可以发现当List存在renderHandler属性不为空时,调用了Handler里的runWith()方法默认为renderHandler的回调函数添加了两个参数。

 

2.关于runWith()方法

this.characterList.renderHandler = Laya.Handler.create(this,this.listRenderHandler,this.text,false);

还是上面的例子,如果在creat()函数里手动给回调函数传入了参数,那么listRenderHandler(cell,index)函数反而不能正常工作,这又是为什么?

检查Handler源码中的runWith()方法:

runWith(data) {
            if (this.method == null)
                return null;
            var id = this._id;
            if (data == null)
                var result = this.method.apply(this.callerthis.args);
            else if (!this.args && !data.unshift)
                result = this.method.call(this.callerdata);
            else if (this.args)
                result = this.method.apply(this.callerthis.args.concat(data));
            else
                result = this.method.apply(this.callerdata);
            this._id === id && this.once && this.recover();
            return result;
        }

可以发现如果我们手动给回调函数传入了参数,这里会使用concat()方法将默认传入的参数拼接到已传入参数的后方(用上面的例子来说就是renderHandler回调函数拥有了三个回调参数,其中第一个时手动指定的,后两个时默认传入的);

只有当没有手动传入任何回调参数时,runWith()方法会将回调函数的参数指定为默认参数,这也说明了为什么我们没有在creat()中传入任何回调参数而却能够使用cell和index这两个默认回调参数

[展开全文]

授课教师

SiKi学院老师

课程特色

下载资料(2)
视频(82)