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

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

价格 ¥ 200.00

获取场景:

Laya.stage.getChildAt(0).getChildAt(0)

[展开全文]

关于Laya.loader.load函数里loadProgress的回调函数:

这个回调函数不能使用Laya.Handler.create(this,func)这种默认的形式,因为这种默认创造Handler的方式会使其调用一次后就被Handler回收器回收,而进度条处理函数需要不断被调用。

所以这里我们只能使用两种创建方式:

1. create(this,func,argus,false)来指定第四个是否调用一次的参数为false,保证不会在调用一次后就被回收。

2. new Laya.Hander()来直接调用构造函数创建一个新Handler对象。

从性能优化角度,推荐使用回收池的形式,也就是第一种方法。

[展开全文]

关于Laya和Cocos父子节点的坑:

对父物体的位置移动和旋转并不会导致其子物体Collider组件位置的移动,必须直接对子物体进行操作才行。

而这点在Unity中是自动同步的。

所以要是在这两个引擎中构建场景:

切记切记,父子物体的嵌套层级不可太多,以最多三层为优

[展开全文]

关于List里的tweenTo()可以有更好的实现方式:

this.owner.getChildByName("btn_Left").on(Laya.Event.CLICK,this,function(){
            this.characterList.tweenTo(this.characterList.startIndex-1,300);
        });
        this.owner.getChildByName("btn_Right").on(Laya.Event.CLICK,this,function(){
            this.characterList.tweenTo(this.characterList.startIndex+1,300);
        });

可以直接通过List.startIndex获得当前list显示内容里的第一项,不必通过scrollBar.value的值间接进行计算。

这样做的好处:

可以避免计算不精确而导致的一系列问题;

可以适配Item项数目有增减的场合

[展开全文]

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)