目录

1.visualcode---配置model快捷模版

2.定义在属性面板显示的变量

3.获取脚本挂载物体组件

4.删除已挂载的脚本,还得在挂载物体上删除引用。

5.代码发生变动,如新增公有变量。需手动切换鼠标选中物体,再选回来才会刷新。

6.定义的公有物体变量node 没有坐标属性,需进行类型转换

7.Laya脚本参数说明

8.Laya事件广播与接收

9.插值

10.勾股定理 求距离

11.随机值封装

12.获取键盘按键

13.代码获取游戏内物体

14.音乐音效播放

15.按钮点击事件监听

16.游戏暂停、场景跳转与重新加载

17.本地存储  相当于unity的PlayerPrefes

18.延时执行

19.设置游戏画面左右居中

20.碰撞检测,多预制体共用lable标签

21.指定时间间隔循环调用

22.预制体加载与生成

23.对象池的获取与回收

24.使用自定义字体

25.音乐音效一键静音

26.时间轴动画---多状态切换

27.tween动画

28.Dialog弹窗

29.判断所在平台

30.数组

31.判断游戏是否在前台 ----防止游戏进入后台后update方法一直执行

32.监听游戏是否失焦


1.visualcode---配置model快捷模版

code---首选项---配置用户代码片段

//例:
{"creat Class":{"scope": "javascript,typescript","prefix": "class","body": ["/**","*","* @ author:xxx","* @ email:371423398@qq.com","* @ data: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE","*/","export default class $TM_FILENAME_BASE extends Laya.Script {","","\tconstructor() {","\t\tsuper();","\t}","\t\t/** @prop {name:xxx, tips:\"面板物体\", type:Node, default:null}*/","\t\t xxx=null;","","\tonAwake() {","\t}","}"],"description": "快速创建一个Laya模板类"}
}

2.定义在属性面板显示的变量

  /** @prop {name:intType, tips:"整数类型示例", type:Int, default:1000}*/public intType: number = 1000;/** @prop {name:numType, tips:"数字类型示例", type:Number, default:1000}*/public numType: number = 1000;/** @prop {name:strType, tips:"字符串类型示例", type:String, default:"hello laya"}*/public strType: string = "hello laya";/** @prop {name:boolType, tips:"布尔类型示例", type:Bool, default:true}*/public boolType: boolean = true;/** @prop {name:shoe,tips:"物体",type:Node,default:null}*/public shoe = null; // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0

3.获取脚本挂载物体组件

this.owner.getComponent(Laya.RigiBody);

4.删除已挂载的脚本,还得在挂载物体上删除引用。

5.代码发生变动,如新增公有变量。需手动切换鼠标选中物体,再选回来才会刷新。

6.定义的公有物体变量node 没有坐标属性,需进行类型转换

2d:
public get gameObject():Laya.Sprite
{return this.owner as Laya.Sprite;
}3d:
public get gameObject():Laya.Sprite3D
{return this.owner as Laya.Sprite3D;
}-------------------------------------------------public get transform():Laya.Transform
{return this.gameObject.transform;
}

7.Laya脚本参数说明

laya脚本参数

8.Laya事件广播与接收

广播事件:

Laya.stage.event("事件名");

接收事件:

onAwake()
{//添加监听Laya.stage.on("事件名",this,this.reset);
}private reset(){//.....
}onDestroy(){//移除监听Laya.stage.off("事件名",this,this.reset);
}

9.插值

Laya.MathUtill.lerp(curObj.x,targetObj.x,Laya.timer.delta/1000 * speed);

10.勾股定理 求距离

a*a + b*b = c*c;c = Math.sqrt(a*a + b*b);

11.随机值封装

    //随机数封装getRandom(min,max){let min = Number(_min);let max = Number(_max);let value = 0;if(max>min){value = Math.random()*(max-min);value = Math.round(value) +min ;}else{value = Math.random()*(min-max);value = Math.round(value) +max ;}return value;}

12.获取键盘按键

1.
onUpdate()
{if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.A)) {this.rig.setVelocity({ x: -10, y: this.rig.linearVelocity.y });} else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.D)) {        this.rig.setVelocity({ x: 10, y: this.rig.linearVelocity.y });}
}2.
onKeyDown(e)
{//console.log(e.nativeEvent.key+".........2");if (e.nativeEvent.key == " " && this.canJump) {this.canJump = false;this.rig.setVelocity({ x: this.rig.linearVelocity.x, y: -13 });}
}3.
onKeyPress(e)
{console.log(e.nativeEvent.key+".........1");if(e.nativeEvent.key == "a"){}
}

13.代码获取游戏内物体

//查找一级物体
Laya.stage.getChildByName("Player");
或
this.owner.parent.getChildByName("wuti");//查找本物体下的子物体
this.owner.getChildByName("子物体名");

14.音乐音效播放

//播放音乐
Laya.soundManager.playMusic(“地址/music.mp3”,0); //0为无限循环//单纯播放音效
Laya.SoundManager.playSound("sound/BallHit-01.mp3");
//播放完成后加事件
Laya.SoundManager.playSound("sound/startWistle.mp3",1,new Laya.Handler(this,()=>{Laya.stage.event("StartGame"); //广播--开始游戏
}));

15.按钮点击事件监听

onAwake()
{this.gameOverPanel.getChildByName("btn_menu").on(Laya.Event.CLICK,this,()=>{//。。。。。            });
}

16.游戏暂停、场景跳转与重新加载

//跳转场景
this.pausePanel.getChildByName("btn_menu").on(Laya.Event.CLICK,this,
()=>{        Laya.Scene.open("Menu.json");
});//继续游戏
this.pausePanel.getChildByName("btn_continue").on(Laya.Event.CLICK,this,
()=>{this.pausePanel.visible = false;this.isPause = false;Laya.timer.scale =1;
});//重新开始
this.pausePanel.getChildByName("btn_restart").on(Laya.Event.CLICK,this,
()=>{Laya.Scene.open("Main.scene");
});/游戏暂停
this.owner.parent.getChildByName("btn_pause").on(Laya.Event.CLICK,this,
()=>{this.isPause = true;this.pausePanel.visible = true;Laya.timer.scale = 0;});

17.本地存储  相当于unity的PlayerPrefes

onEnable(): void {if(Laya.LocalStorage.getItem("headIndex")=="NaN"){Laya.LocalStorage.setItem("headIndex","1");}this.head_index = parseInt(Laya.LocalStorage.getItem("headIndex"));this.skinUrl = "Textures/Players/Player-Head-0"+this.head_index+"-n.png";this.roleIcon.skin = this.skinUrl;
}

18.延时执行

//清除此脚本已存在的timer事件
Laya.timer.clearAll(this);
//延迟0.5秒执行
Laya.timer.once(500, this, () => {this.heroIcon.texture = "Textures/Players/Player-Head-01-n.png";
});

19.设置游戏画面左右居中

//场景分辨率 1920*1080
Laya.stage.pivot(960, 0);
Laya.stage.x = Laya.stage.width / 2;

20.碰撞检测,多预制体共用lable标签

选中物体下的collider组件,给lable属性命名,后续即可使用lable名判定碰撞物体 

21.指定时间间隔循环调用

onAwake() {this.ranTime = this.getRandom(300,800);Laya.timer.loop(this.ranTime,this,()=>{this.carSpawn();this.ranTime = this.getRandom(300,800);});
}

22.预制体加载与生成

    //预制体carFab = [];//预制体加载地址carInfo = [];//预制体存放路径carPath = ["prefab/car_1.json","prefab/car_2.json","prefab/car_3.json","prefab/car_4.json","prefab/car_5.json","prefab/car_6.json",];onAwake() {this.loadCarPrefab();}private loadCarPrefab(){//获取预制体加载信息for(let i=0;i<this.carPath.length;i++){this.carInfo.push({url:this.carPath[i],type:Laya.Loader.PREFAB});}//进行加载Laya.loader.load(this.carInfo,Laya.Handler.create(this,(ret)=>{if(ret){//加载完成,获取预制体for(let i=0;i<this.carPath.length;i++){this.carFab.push(Laya.loader.getRes(this.carPath[i]));}//生成    this.ranTime = this.getRandom(300,800);Laya.timer.loop(this.ranTime,this,()=>{this.carSpawn();this.ranTime = this.getRandom(300,800);});}}));}private carSpawn() {this.carInitX = this.initArray[this.getRandom(0, this.initArray.length - 1)];let carIndex = this.getRandom(0, this.carPath.length-1);    //使用对象池获取对象let car = Laya.Pool.getItemByCreateFun(carIndex.toString(),()=>{return this.carFab[carIndex].create();},this);Laya.stage.addChild(car);car.pos(this.carInitX, this.carInitY);}

23.对象池的获取与回收

//GameManager.ts
private carSpawn() {this.carInitX = this.initArray[this.getRandom(0, this.initArray.length - 1)];let carIndex = this.getRandom(0, this.carPath.length-1);//使用对象池获取对象let car = Laya.Pool.getItemByCreateFun(carIndex.toString(),()=>{return this.carFab[carIndex].create();},this);Laya.stage.addChild(car);car.pos(this.carInitX, this.carInitY);//设置对象池回收标记car.getComponent(CarControl).Init(carIndex.toString());
}//Car.ts//对象池回收标识符private sign = null;Init(_sign){this.sign = _sign;}onTriggerExit(other){switch(other.owner.name){case "bottomCollider"://从场景中移除自己this.owner.removeSelf();//回收Laya.Pool.recover(this.sign,this.owner);break;}}

24.使用自定义字体

 private txt_score;onAwake(){this.txt_score = this.owner.getChildByName("txt_score");Laya.loader.load("hemi head bd it.ttf",Laya.Handler.create(this,(font)=>{this.txt_score.font = font.fontName;}),null,Laya.Loader.TTF);}

25.音乐音效一键静音

//静音
Laya.SoundManager.muted = true;//打开声音
Laya.SoundManager.muted = false;

26.时间轴动画---多状态切换

export default class BirdContr extends Laya.Script {constructor() { super(); }private rig = null;private ownBird =null;onAwake(): void {this.ownBird = this.owner;this.rig = this.owner.getComponent(Laya.RigidBody);Laya.stage.on(Laya.Event.CLICK,this,this.onMouseClick);}onMouseClick(): void {this.rig.linearVelocity = {x:0,y:-10};this.ownBird.autoAnimation = "fly";this.ownBird.loop = false;}onUpdate(){if(this.ownBird.isPlaying==false){this.ownBird.autoAnimation = "idle";}}onTriggerEnter(other) {console.log(other.owner.name);switch (other.owner.name) {case "ground1":case "ground2":this.ownBird.autoAnimation = "die";isGameOver = true;break;}}
}

27.tween动画

showGameOver(){this.overPanel.visible = true;//参数1:目标物体  参数2:目标属性  参数3:持续时间 参数4:动画类型  参数5:回调 --不需要可以不写Laya.Tween.from(this.overPanel,{alpha:0},500,Laya.Ease.circIn,new         Laya.Handler(this,()=>{}));}onAwake(){this.overPanel = this.owner.getChildByName("gameOver");this.overPanel.getChildByName("btn_restart").on(Laya.Event.CLICK,this,()=>{Laya.Tween.to(this.overPanel,{alpha:0},500,Laya.Ease.circIn,new Laya.Handler(this,()=>{//this.overPanel.alpha = 0;this.overPanel.visible = false;this.init();Laya.Scene.open("Main.json");}));});}

28.Dialog弹窗

关键字:详情

29.判断所在平台

if(Laya.Browser.onWeiXin){//...
}

30.数组

arrayA = ['bbb','ccc'];

push ---  在数组 后添加新数据。     arrayA.push('ddd');       // ['bbb','ccc','ddd']

unshift --- 在数组 前添加新数据。    arrayA.unshift('aaa');   //['aaa','bbb','ccc','ddd']

splice() --- 有两个参数时删除,否则是添加:

1.指定下标位置进行添加:

arrayA.splice(2,0,'mm',"nn");     // ['aaa','bbb','mm','nn','ccc','ddd']

2.指定下标位置进行删除:

arrayA.splice(1,2);          //['aaa','nn','ccc','ddd']

pop() 删除掉最后的一个元素。   arrayA.pop();  // ['aaa','nn','ccc']

shift() 删除掉第一个元素。        arrayA.shift();   //['nn','ccc']

31.判断游戏是否在前台 ----防止游戏进入后台后update方法一直执行

Laya.stage.isVisibility

32.监听游戏是否失焦

Laya.stage.on(Laya.Event.BLUR,this,()=>{ConstScript.updateStop = true;Laya.timer.scale = 0;});Laya.stage.on(Laya.Event.FOCUS,this,()=>{ConstScript.updateStop = false;Laya.timer.scale = 1;});

LayaBox---知识点相关推荐

  1. 解释型语言与编译型的必须知识点

    解释型语言与编译型的必须知识点 概念: 计算机不能理解直接理解高级语言,只能理解机器语言,所以必须把高级语言翻译成机器语言,计算机才能执行高级语言编写的程序. 翻译的方式有两种: 编译 解释 两种翻译 ...

  2. YOLOV4知识点分析(二)

    YOLOV4知识点分析(二) 数据增强相关-mixup 论文名称:mixup: BEYOND EMPIRICAL RISK MINIMIZATION 论文地址:https://arxiv.org/ab ...

  3. YOLOV4知识点分析(一)

    YOLOV4知识点分析(一) 简 介 yolov4论文:YOLOv4: Optimal Speed and Accuracy of Object Detection arxiv:https://arx ...

  4. 你需要掌握的有关.NET DateTime类型的知识点和坑位 都在这里

    引言    DateTime数据类型是一个复杂的问题,复杂到足以让你在编写[将日期从Web服务器返回到浏览器]简单代码时感到困惑. ASP.NET MVC 5和 Web API 2/ASP.NETCo ...

  5. 简练软考知识点整理-范围确认易混概念

    与确认范围容易混淆的知识点包括,确认范围与核实产品.质量控制.项目收尾,下面进行比较分析. (1)确认范围与核实产品 核实产品是针对产品是否完成,在项目(或阶段)结束时由发起人或客户来验证,强调产品是 ...

  6. 朴素贝叶斯知识点概括

    1. 简述 贝叶斯是典型的生成学习方法 对于给定的训练数据集,首先,基于特征条件独立假设,学习输入/输出的联合概率分布:然后,基于此模型,对于给定的输入x,根据贝叶斯定理求后验概率最大的输出y 术语说 ...

  7. 计算机二级函数知识,2017年全国计算机二级考试MS Office高级应用知识点:INDIRECT函数...

    INDIRECT函数知识点 适用考试:全国计算机二级考试 考试科目:MS Office高级应用 科目知识点:INDIRECT函数 INDIRECT函数立即对引用进行计算,并显示其内容.当需要更改公式中 ...

  8. python如何创建一个类_python (知识点:类)简单的创建一个类

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Mon Nov 14 01:01:29 2016 ...

  9. 全国计算机二级vfp知识点,全国计算机二级VFP知识点总结

    全国计算机二级 Visual FoxPro 数据库程序设计 --知识点整理资料 文件扩展名及备注文件扩展名 文件 项目 表 程序 单索引 查询 菜单定义格式 扩展名 .pjx .dbf .prg .i ...

  10. golang sdk后端怎么用_Golang资深后端工程师需要了解的知识点

    前提: 因近段时间,我在考虑新的工作机会,并在自己的以往的工作内容做了一些简单的总结,以及部分在面试过程当中遇到了一些新的问题,总结一篇关于Golang工程师针对后端开发的一些知识点. 本文仅作为参考 ...

最新文章

  1. iphonex重量_精仿苹果iPhone X手机配置介绍
  2. LeetCode-笔记-57.插入区间
  3. Android热补丁技术—dexposed原理简析(手机淘宝采用方案)
  4. Oracle数据库表设计时的注意事项
  5. iPhone12机型判断
  6. stm32f4 RAM中运行程序 读保护设置
  7. foundation of the academics
  8. union all动态表_Excel VBA——动态显示图表
  9. 1007. Maximum Subsequence Sum (25)
  10. Redis 笔记之 Java 操作 Redis(Jedis)
  11. 获取数据库链接Junit
  12. 名言名人2008-11-22
  13. Redis源码分析系列十一:createClient后面内容
  14. 制造型企业呼叫中心搭建-SDCC呼叫中心
  15. 玩和平精英跟刺激战场国际服都被吊打?网友:你还可以玩荒野行动
  16. JQuery 属性操作 - attr() 方法
  17. 什么软件能测试gps高度,‎App Store: GPS海拔测量仪-实时高度测量海拔表
  18. Linux安装和部署
  19. Python电影观众数量回归分析 随机森林 可视化 实验报告
  20. 線上 Android/Linux Kernel Source Code瀏覽 - Android/Linux Source Code Cross Reference

热门文章

  1. DX11与多线程渲染
  2. 74LS138的结构
  3. MATLAB实现短时傅里叶变换
  4. 计算机大类专业学习c语言之重要性
  5. 中投 汇金 中金 中登
  6. python function terminated un_python僵尸进程产生的原因
  7. linux bridge vlan,Linux Bridge实现Vlan
  8. 想知道PDF转Word软件哪个好?向你推荐3个自用软件
  9. 理光2014ad扫描服务器响应,理光mp2014ad扫描驱动和打印驱动
  10. 信息安全管理体系ISO27001IT服务管理体系ISO20000(转)