• LayaBox---知识点


    目录

    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.对象池的获取与回收


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

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

    1. //例:
    2. {
    3. "creat Class":{
    4. "scope": "javascript,typescript",
    5. "prefix": "class",
    6. "body": [
    7. "/**",
    8. "*",
    9. "* @ author:xxx",
    10. "* @ email:371423398@qq.com",
    11. "* @ data: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE",
    12. "*/",
    13. "export default class $TM_FILENAME_BASE extends Laya.Script {",
    14. "",
    15. "\tconstructor() {",
    16. "\t\tsuper();",
    17. "\t}",
    18. "\t\t/** @prop {name:xxx, tips:\"面板物体\", type:Node, default:null}*/",
    19. "\t\t xxx=null;",
    20. "",
    21. "\tonAwake() {",
    22. "\t}",
    23. "}"
    24. ],
    25. "description": "快速创建一个Laya模板类"
    26. }
    27. }

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

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

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

    this.owner.getComponent(Laya.RigiBody);

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

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

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

    1. 2d:
    2. public get gameObject():Laya.Sprite
    3. {
    4. return this.owner as Laya.Sprite;
    5. }
    6. 3d:
    7. public get gameObject():Laya.Sprite3D
    8. {
    9. return this.owner as Laya.Sprite3D;
    10. }
    11. -------------------------------------------------
    12. public get transform():Laya.Transform
    13. {
    14. return this.gameObject.transform;
    15. }

    7.Laya脚本参数说明

    laya脚本参数

    8.Laya事件广播与接收

    广播事件:

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

    接收事件:

    1. onAwake()
    2. {
    3. //添加监听
    4. Laya.stage.on("事件名",this,this.reset);
    5. }
    6. private reset(){
    7. //.....
    8. }
    9. onDestroy(){
    10. //移除监听
    11. Laya.stage.off("事件名",this,this.reset);
    12. }

    9.插值

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

    10.勾股定理 求距离

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

    11.随机值封装

    1. //随机数封装
    2. private getRandom(min, max)
    3. {
    4. let value = Math.random()*(max-min);
    5. value = Math.round(value);
    6. return value + min;
    7. }

    12.获取键盘按键

    1. 1.
    2. onUpdate()
    3. {
    4. if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.A))
    5. {
    6. this.rig.setVelocity({ x: -10, y: this.rig.linearVelocity.y });
    7. } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.D))
    8. {
    9. this.rig.setVelocity({ x: 10, y: this.rig.linearVelocity.y });
    10. }
    11. }
    12. 2.
    13. onKeyDown(e)
    14. {
    15. //console.log(e.nativeEvent.key+".........2");
    16. if (e.nativeEvent.key == " " && this.canJump)
    17. {
    18. this.canJump = false;
    19. this.rig.setVelocity({ x: this.rig.linearVelocity.x, y: -13 });
    20. }
    21. }
    22. 3.
    23. onKeyPress(e)
    24. {
    25. console.log(e.nativeEvent.key+".........1");
    26. if(e.nativeEvent.key == "a")
    27. {
    28. }
    29. }

    13.代码获取游戏内物体

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

    14.音乐音效播放

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

    15.按钮点击事件监听

    1. onAwake()
    2. {
    3. this.gameOverPanel.getChildByName("btn_menu").on
    4. (Laya.Event.CLICK,this,()=>{
    5. //。。。。。
    6. });
    7. }

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

    1. //跳转场景
    2. this.pausePanel.getChildByName("btn_menu").on(Laya.Event.CLICK,this,
    3. ()=>{
    4. Laya.Scene.open("Menu.json");
    5. });
    6. //继续游戏
    7. this.pausePanel.getChildByName("btn_continue").on(Laya.Event.CLICK,this,
    8. ()=>{
    9. this.pausePanel.visible = false;
    10. this.isPause = false;
    11. Laya.timer.scale =1;
    12. });
    13. //重新开始
    14. this.pausePanel.getChildByName("btn_restart").on(Laya.Event.CLICK,this,
    15. ()=>{
    16. Laya.Scene.open("Main.json");
    17. });
    18. /游戏暂停
    19. this.owner.parent.getChildByName("btn_pause").on(Laya.Event.CLICK,this,
    20. ()=>{
    21. this.isPause = true;
    22. this.pausePanel.visible = true;
    23. Laya.timer.scale = 0;
    24. });

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

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

    18.延时执行

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

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

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

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

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

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

    1. onAwake() {
    2. this.ranTime = this.getRandom(300,800);
    3. Laya.timer.loop(this.ranTime,this,()=>{
    4. this.carSpawn();
    5. this.ranTime = this.getRandom(300,800);
    6. });
    7. }

    22.预制体加载与生成

    1. //预制体
    2. carFab = [];
    3. //预制体加载地址
    4. carInfo = [];
    5. //预制体存放路径
    6. carPath = [
    7. "prefab/car_1.json",
    8. "prefab/car_2.json",
    9. "prefab/car_3.json",
    10. "prefab/car_4.json",
    11. "prefab/car_5.json",
    12. "prefab/car_6.json",
    13. ];
    14. onAwake() {
    15. this.loadCarPrefab();
    16. }
    17. private loadCarPrefab(){
    18. //获取预制体加载信息
    19. for(let i=0;i<this.carPath.length;i++){
    20. this.carInfo.push({url:this.carPath[i],type:Laya.Loader.PREFAB});
    21. }
    22. //进行加载
    23. Laya.loader.load(this.carInfo,Laya.Handler.create(this,(ret)=>{
    24. if(ret){
    25. //加载完成,获取预制体
    26. for(let i=0;i<this.carPath.length;i++){
    27. this.carFab.push(Laya.loader.getRes(this.carPath[i]));
    28. }
    29. //生成
    30. this.ranTime = this.getRandom(300,800);
    31. Laya.timer.loop(this.ranTime,this,()=>{
    32. this.carSpawn();
    33. this.ranTime = this.getRandom(300,800);
    34. });
    35. }
    36. }));
    37. }
    38. private carSpawn() {
    39. this.carInitX = this.initArray[this.getRandom(0, this.initArray.length - 1)];
    40. let carIndex = this.getRandom(0, this.carPath.length-1);
    41. //使用对象池获取对象
    42. let car = Laya.Pool.getItemByCreateFun(carIndex.toString(),()=>{
    43. return this.carFab[carIndex].create();
    44. },this);
    45. Laya.stage.addChild(car);
    46. car.pos(this.carInitX, this.carInitY);
    47. }

    23.对象池的获取与回收

    1. //GameManager.ts
    2. private carSpawn() {
    3. this.carInitX = this.initArray[this.getRandom(0, this.initArray.length - 1)];
    4. let carIndex = this.getRandom(0, this.carPath.length-1);
    5. //使用对象池获取对象
    6. let car = Laya.Pool.getItemByCreateFun(carIndex.toString(),()=>{
    7. return this.carFab[carIndex].create();
    8. },this);
    9. Laya.stage.addChild(car);
    10. car.pos(this.carInitX, this.carInitY);
    11. //设置对象池回收标记
    12. car.getComponent(CarControl).Init(carIndex.toString());
    13. }
    14. //Car.ts
    15. //对象池回收标识符
    16. private sign = null;
    17. Init(_sign)
    18. {
    19. this.sign = _sign;
    20. }
    21. onTriggerExit(other){
    22. switch(other.owner.name){
    23. case "bottomCollider":
    24. //从场景中移除自己
    25. this.owner.removeSelf();
    26. //回收
    27. Laya.Pool.recover(this.sign,this);
    28. break;
    29. }
    30. }

    24.使用自定义字体

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

    25.音乐音效一键静音

    1. //静音
    2. Laya.SoundManager.muted = true;
    3. //打开声音
    4. Laya.SoundManager.muted = false;

  • 相关阅读:
    【Autosar 存储栈Memery Stack 3.存储读写流程的要求与时序】
    秒杀系统设计(分布式微服务)
    阿桂天山的技术小结:Flask对Ztree树节点搜索定位
    动手学深度学习—批量规范化(代码详解)
    大数据开发工程师必备技能有哪些?
    网络原理 - HTTP/HTTPS(4)
    [HDLBits] Countbcd
    (附源码)计算机毕业设计SSM基于的民宿租赁系统
    Liunx文件操作命令(touch、cat、vim、more、less、cp、mv、rm、head、tail、file、find)
    Elasticsearch基础增删改查
  • 原文地址:https://blog.csdn.net/lalate/article/details/126154659