目录
5.代码发生变动,如新增公有变量。需手动切换鼠标选中物体,再选回来才会刷新。
6.定义的公有物体变量node 没有坐标属性,需进行类型转换
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模板类"
- }
- }
- /** @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
this.owner.getComponent(Laya.RigiBody);
- 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;
- }
广播事件:
Laya.stage.event("事件名");
接收事件:
- onAwake()
- {
- //添加监听
- Laya.stage.on("事件名",this,this.reset);
- }
-
- private reset(){
- //.....
- }
-
- onDestroy(){
- //移除监听
- Laya.stage.off("事件名",this,this.reset);
- }
Laya.MathUtill.lerp(curObj.x,targetObj.x,Laya.timer.delta/1000 * speed);
- a*a + b*b = c*c;
-
- c = Math.sqrt(a*a + b*b);
- //随机数封装
- private getRandom(min, max)
- {
- let value = Math.random()*(max-min);
- value = Math.round(value);
- return value + min;
- }
- 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")
- {
- }
- }
- //查找一级物体
- Laya.stage.getChildByName("Player");
- 或
- this.owner.parent.getChildByName("wuti");
-
- //查找本物体下的子物体
- this.owner.getChildByName("子物体名");
- //播放音乐
- 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"); //广播--开始游戏
- }));
- onAwake()
- {
- this.gameOverPanel.getChildByName("btn_menu").on
- (Laya.Event.CLICK,this,()=>{
- //。。。。。
- });
- }
- //跳转场景
- 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.json");
- });
-
- /游戏暂停
- this.owner.parent.getChildByName("btn_pause").on(Laya.Event.CLICK,this,
- ()=>{
- this.isPause = true;
- this.pausePanel.visible = true;
- Laya.timer.scale = 0;
- });
- 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;
- }
- //清除此脚本已存在的timer事件
- Laya.timer.clearAll(this);
- //延迟0.5秒执行
- Laya.timer.once(500, this, () => {
- this.heroIcon.texture = "Textures/Players/Player-Head-01-n.png";
- });
- //场景分辨率 1920*1080
- Laya.stage.pivot(960, 0);
- Laya.stage.x = Laya.stage.width / 2;
选中物体下的collider组件,给lable属性命名,后续即可使用lable名判定碰撞物体 
- onAwake() {
- this.ranTime = this.getRandom(300,800);
- Laya.timer.loop(this.ranTime,this,()=>{
- this.carSpawn();
- this.ranTime = this.getRandom(300,800);
- });
- }
- //预制体
- 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);
- }
- //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);
- break;
- }
- }
- 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);
- }
- //静音
- Laya.SoundManager.muted = true;
-
- //打开声音
- Laya.SoundManager.muted = false;