本文以切入源码的方式讲解laya核心API的使用以及需要注意的点。 Node(节点)操作 大部分的laya UI组件都可以看做节点,可以看做web开发中,使用JavaScript对html节点进行操作。 添加节点核心方法: addChild(node: Node): Node; addChildren(...args: any[]): void; addChildAt(node: Node, index: number): Node; 注意:添加节点时候,越后添加的层级越高,可以设置zOrder属性调整对象层级。 销毁、删除节点核心方法: removeChild(node: Node): Node; removeSelf(): Node; removeChildByName(name: string): Node; removeChildAt(index: number): Node; removeChildren(beginIndex?: number, endIndex?: number): Node; destroy(destroyChild?: boolean): void; destroyChildren(): void; 注意:如果将节点销毁,则不能再使用,但删除之后的节点依旧可以使用。 获取节点核心方法: getChildIndex(node: Node): number; getChildByName(name: string): Node; 注意:getChildByName方法传入的name,是在创建的时候为节点设置的name,并且只能获取下一级的孩子节点,而不是所有属于当前节点的所有子节点。 事件事件类型非常多,常用的有: static MOUSE_DOWN: string; static MOUSE_UP: string; static CLICK: string; static RIGHT_MOUSE_DOWN: string; static RIGHT_MOUSE_UP: string; static RIGHT_CLICK: string; static MOUSE_MOVE: string; static MOUSE_OVER: string; 具体可以查看layaAir.d.ts laya.events模块,大概在17316行....... 事件绑定: on(type: string, caller: any, listener: Function, args?: Array<any>): EventDispatcher; once(type: string, caller: any, listener: Function, args?: Array<any>): EventDispatcher; 注意:多次为同一个节点使用on方法会多次绑定事件,需要了解的是,如果对节点的操作不会有变化,则使用on方法绑定一次即可,如果根据状态条件等因素的不同,则使用once绑定。 网络网络请求也是封装的HttpRequest,比较简单。 send(url: string, data?: any, method?: string, responseType?: string, headers?: Array<any>): void; 一个简单的请求网络数据示例: getList(page,size,url){ let http = new Laya.HttpRequest(); http.send(`${this.gd.host}${url}?userId=${this.gd.user.id}&page=${page}&size=${size}`, 'GET'); http.timeout = 1000; return new Promise((resolve, reject) => { http.once(Laya.Event.COMPLETE, this, (resp) => { resp = JSON.parse(resp); if (resp.meta.code === 200) { if (resp.data !== null) { resolve(resp.data) } else { resolve(resp.data); } } else { console.log('服务器错误'); } }); http.once(Laya.Event.ERROR, this, (resp) => { resolve(resp.data); }); }) } let data = await EventUtils.getInstance().getList(this.currentPage, this.size,'/norain/pre/rank/time'); 动画缓动动画 缓动类型解释传送门:https://layaair.ldc./api/?category=Core&class=laya.utils.Ease 缓动动画演示传送门:https://layaair.ldc./demo/?category=2d&group=Tween&name=EaseFunctionsDemo 拓展阅读: 深入理解遗传算法(一) 深入理解遗传算法(二)
从1到100求和学算法思维(一) 从1到100求和学算法思维(二)
从1到100求和学算法思维(三)
从1到100求和学算法思维(四)
从1到100求和学算法思维(五) 从1到100求和学算法思维(六)
|