从0开始用CocosCreator开发飞机大战小游戏(十四)

157 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第22天,点击查看活动详情

上次说到了物资的产生和移动,今天说下玩家飞机吃到物资后的反应。

1. 打开goods.ts脚本。

(1)在onLoad函数里注册碰撞回调函数

     let collider=this.node.getComponent(Collider2D)

        if(collider){

            collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)

        }

(2)注册,记得引入Collider2D,Contact2DType,IPhysics2DContact image.png

(3)引入play脚本

import { play } from './play';

(4)增加碰撞回调函数     onBeginContact(selfCollider:Collider2D,otherCollider:Collider2D,contact:IPhysics2DContact | null){

        //判断吃到物资后的反应

        if(this.goodsType==global.BLOODGOODS){

          otherCollider.node.getChildByName("blood").getComponent(ProgressBar).progress=1;           otherCollider.node.getComponent(play).planeBlood=otherCollider.node.getComponent(play).planeTotalBlood;

        }else if(this.goodsType==global.LIGHTGOODS){

            otherCollider.node.getComponent(play).isshootLight=true;

        }else if(this.goodsType==global.MISSILEGOODS){

            otherCollider.node.getComponent(play).isshootMissile=true;

        }

        this.goodsFactory.recycleProduct(this.node)

    }

(5)注意,之前脚本play.ts里的是否发射激光子弹和是否发射导弹子弹为了方便调试是设置为true的,现在要改为false.

    isshootLight:boolean=false//是否发射激光子弹

    isshootMissile:boolean=false  //是否发射导弹子弹

2. 接下来说说敌机与玩家飞机碰撞。

(1)打开项目设置中的物理选项,选中敌机可以与玩家飞机碰撞 image.png

(2)打开脚本enemy.ts,在onLoad函数里注册碰撞回调函数

   let collider=this.node.getComponent(Collider2D)

        if(collider){

            collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)

        }

(3)同样,也要引入Collider2D,Contact2DType,IPhysics2DContact image.png

(4)因为敌机会与玩家飞机碰撞,又会与玩家发生碰撞,为了区分两者,因些在玩家飞机节点上的Tag 设置为1

image.png

(5)添加碰撞回调函数   onBeginContact(selfCollider:Collider2D,otherCollider:Collider2D,contact:IPhysics2DContact | null){

        if(otherCollider.tag==1){ //如果碰撞的是玩家飞机

            otherCollider.getComponent(play).planeBlood-=50;

            //更新血条

            otherCollider.node.getChildByName("blood").getComponent(ProgressBar).progress=otherCollider.getComponent(play).planeBlood/otherCollider.getComponent(play).planeTotalBlood

            this.enemyFactory.recycleProduct(this.node)

        }

    }

3. 接下来说下得分功能。

(1)打开game场景,在Canvas添加一个Label(文本),并且命名为score   image.png

(2)String默认为score.0 image.png

(3)添加一个对齐挂件的组件,UI下的Widget image.png

(4) 调整位置 image.png

(6)打开脚本global.ts,添加score

   public static SCORE:number=0

(7) 打开脚本enemy.ts,在碰撞回调函数增加添加分数函数功能。当玩家去碰撞敌机时,敌机消失时增加分数

    onBeginContact(selfCollider:Collider2D,otherCollider:Collider2D,contact:IPhysics2DContact | null){

        if(otherCollider.tag==1){ //如果碰撞的是玩家飞机

            otherCollider.getComponent(play).planeBlood-=50;

            //更新血条             otherCollider.node.getChildByName("blood").getComponent(ProgressBar).progress=otherCollider.getComponent(play).planeBlood/otherCollider.getComponent(play).planeTotalBlood

                        //添加分数

            if(this.enemyType==global.ENEMY_1){

                global.SCORE+=20

            }else{

                global.SCORE+=40

            }

            find("Canvas/score").getComponent(Label).string="Score"+global.SCORE.toString()

            this.enemyFactory.recycleProduct(this.node)

        }

(8)打开playerBullet.ts脚本,在碰撞回调函数里,当敌机消失时,增加分数

         if(otherCollider.node.getComponent(enemy).enemyType==global.ENEMY_1){

                global.SCORE+=20;

            }else{

                global.SCORE+=40;    

            }

            find("Canvas/score").getComponent(Label).string="Score"+global.SCORE.toString()   image.png 今天就到这里了,主要说了增加分数的功能。下次我们再说说游戏结束。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家