Laya2.0初探1——水管鸟案例

前言

本文初探LayaAir2.0引擎,记录学习历程

其实学习Laya引擎是很早之前定下的目标。但有一些事情耽搁了就晚了一些时间。这两天初窥门径,当然期间也踩过不少的坑。我的想法是先从Laya的2D游戏下手,老项目——先使用Laya完成水管鸟实操再说。


1:循环滚动的背景图

在CocosCreator的学习日志里,我也写过这个功能,但在creator中我是在update中直接缩减节点的x来实现节点向左移动的效果,在Laya中我使用的是刚体通过设置linearVelocity来设置刚体的线性速度来控制移动。

首先创建两个相同的背景,并设置好宽度、高度,以及位置,然后为两个节点都添加好碰撞组件(添加碰撞组件时会自动添加上刚体组件)

接下来在工程面板中的Scripts文件夹下新建BgMove.js文件,并将他拖到Scene上面

/**
*
* @ author:QQ
* @ email:1506978348@qq.com
* @ data: 2020-10-15 20:10
*/
export default class BgMove extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:moveSpeed,tips:"提示文本",type:Number,default:null}*/
        this.moveSpeed=5;
    }

    onAwake(){//组件激活执行<onLoad>
        this.bg1 = this.owner.getChildByName("bg1");
        this.bg1.getComponent(Laya.RigidBody).linearVelocity = {x:-this.moveSpeed,y:0};
        this.bg2 = this.owner.getChildByName("bg2");
        this.bg2.getComponent(Laya.RigidBody).linearVelocity = {x:-this.moveSpeed,y:0};
        // this.ground1 = this.owner.getChildByName("ground1");
        // this.ground1.getComponent(Laya.RigidBody).linearVelocity = {x:-this.moveSpeed,y:0};
        // this.ground2 = this.owner.getChildByName("ground2");
        // this.ground2.getComponent(Laya.RigidBody).linearVelocity = {x:-this.moveSpeed,y:0};
    }
    onUpdate(){//每帧执行
        if(this.bg1.x<=-this.bg1.width){
            this.bg1.x += this.bg1.width*2;
        }
        if(this.bg2.x<=-this.bg2.width){
            this.bg2.x += this.bg2.width*2;
        }
        // if(this.ground1.x<=-this.ground1.width){
        //     this.ground1.x += this.ground1.width*2;
        // }
        // if(this.ground2.x<=-this.ground2.width){
        //     this.ground2.x += this.ground2.width*2;
        // }
        
    }
}

接下来,可以点击LayaIDE中的运行按钮在Chrome中运行(若报错提示如下图)

修改方式:打开VsCode,将工程中的.vscode文件夹下的launch.json文件打开并修改其中“runtimeExecutable”的参数为Chrome浏览器安装路径(默认状态为已注释);

接下来,你就可以在Chrome浏览器中调试了,打开Chrome浏览器,你会发现你的背景已经开始动起来了

2:添加飞机动画并点击控制飞机的上下移动

在Laya里面的动画编辑器和cocos里面的完全不同,所以我目前也只是了解了一个大概,能用就行

在层级管理器下的Scene中新建一个Animation组件

将需要的动画素材按顺序全选(按住Ctrl键,鼠标左键选中需要的素材),然后将他们拖到动画组件中的source中,接着将autoPlay设置为true

为小飞机添加碰撞组件,并修改BoxCollider的宽高使其将小飞机主体包围住。

接着新建点击控制飞机上移的脚本,然后挂载在飞机动画上

/**
*
* @ author:小学僧
* @ email:1506978348@qq.com
* @ data: 2020-10-20 20:12
*/

export default class planeFly extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:name,tips:"提示文本",type:Node,default:null}*/
        this.xx=null;
    }

    onAwake(){
        Laya.stage.on(Laya.Event.CLICK,this,this.click);
    }
    click(){
        this.owner.getComponent(Laya.RigidBody).linearVelocity = {x:0,y:-10};
        
    }
}

然后在Chrome浏览器中运行,就可以使用鼠标的点击来控制飞机的向上移动了。

3:添加飞机的碰撞脚本

在实际的游戏中,飞机碰到地面,碰到山是会造成游戏结束的,所以这里需要为飞机添加碰撞检测的脚本

首先添加一个防止飞机向上飞出屏幕的碰撞体并设置它的RigidBody类型为Static(静态),接着在飞机移动的脚本中添加碰撞检测的代码

/**
*
* @ author:小学僧
* @ email:1506978348@qq.com
* @ data: 2020-10-20 20:12
*/
let gameOver = false;
export default class planeFly extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:name,tips:"提示文本",type:Node,default:null}*/
        this.xx=null;
    }

    onAwake(){
        Laya.stage.on(Laya.Event.CLICK,this,this.click);
    }
    click(){
        if(gameOver)return;
        this.owner.getComponent(Laya.RigidBody).linearVelocity = {x:0,y:-10};
        
    }
    onTriggerEnter(other){
        if(other.owner.name == 'sky'){
            return;
        }
        this.owner.loop = false;
        gameOver = true;
        Laya.stage.event("gameOver");//注册事件
    }
}

由于当飞机碰到山体游戏结束后,场景就不再移动,那么需要在Scene上面挂载的脚本进行注册事件的判断,在该脚本中onAwake方法中简单判断注册事件然后给背景刚体的线性速度设置为0即可。

Laya.stage.on("gameOver", this, function () {
            this.bg1.getComponent(Laya.RigidBody).linearVelocity = { x: 0, y: 0 };
            this.bg2.getComponent(Laya.RigidBody).linearVelocity = { x: 0, y: 0 };
            this.ground1.getComponent(Laya.RigidBody).linearVelocity = { x: 0, y: 0 };
            this.ground2.getComponent(Laya.RigidBody).linearVelocity = { x: 0, y: 0 };
        });

4:制作山体预制体

游戏中我们的山体都是通过一个山体来进行动态生成的。因此需要制作一个山体预制体来不断实例化

为山体节点添加碰撞组件,然后修改碰撞组件的范围,使其包裹住山体,并将其刚体类型设置为kinematic。接着因为山体要向左移动,所以需要新建一个脚本挂载在山体上来控制其移动

/**
*
* @ author:小学僧
* @ email:1506978348@qq.com
* @ data: 2020-10-23 19:50
*/
export default class wallMove extends Laya.Script {

    constructor() {
        super()
    }

    onAwake(){
        this.owner.getComponent(Laya.RigidBody).linearVelocity = {x:-5,y:0}
        Laya.stage.on("gameOver",this,function () {
            this.owner.getComponent(Laya.RigidBody).linearVelocity = {x:0,y:0}
          })
    }
}

接着就将做好的一个山体做成预制体

首先点击右上角的存盘功能,然后输入预制体名称点击确定就可以在Scene文件夹下看见一个prefab文件夹,我们做好的预制体就在里面。

constructor() {
        super();
        /** @prop {name:wallPrefab,type:Prefab,default:null}*/
        this.wallPrefab;
    }

接着在Scene挂载的脚本中添加可挂载Prefab的代码,然后再IDE中将预制体拖拽到脚本上。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用和提到了关于Laya的组件和使用方法,而引用则是一个关于ComboBoxText组件的代码示例。 Laya的datasource是指为组件提供数据源的属性或方法。具体来说,在Laya中,通过给组件设置datasource属性或者调用setDataSource方法,可以将数据源绑定到相应的组件上。 在以上提供的引用中,并没有直接涉及到Laya的datasource相关内容。如果您有关于Laya的datasource的具体问题,请提供更详细的信息,我会尽力帮助您解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [layabox:小技巧及注意事项汇总(不定时更新)](https://blog.csdn.net/u013119612/article/details/79733327)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Laya实现滑动列表和翻页效果及优化](https://blog.csdn.net/qq_37904209/article/details/104760702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Laya学习笔记-13ComboBox](https://blog.csdn.net/a451319296/article/details/106463754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蟹 !

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值