初学者用COCOS CREATOR写一款飞机大战游戏(6)

您所在的位置:网站首页 游戏类型细分 初学者用COCOS CREATOR写一款飞机大战游戏(6)

初学者用COCOS CREATOR写一款飞机大战游戏(6)

2023-06-09 23:35| 来源: 网络整理| 查看: 265

在之前的文章中我们介绍了玩家管理类和UI方面的一些内容,这篇文章我们着重来介绍一下敌人管理类,游戏里戒戒写了一个所有敌人的基类,由于游戏比较简单,也没有再做具体的细分。

每一个敌人都有一些共同的属性,startImpactAttack撞击攻击力、初始血量startHealth、初始速度startSpeed、默认旋转速度rotateSpeed等等。一般来说敌人我们会做成预设体的类型,然后这些属性都可以维护在预设体的脚本中。

每一个敌人也会有一个生命进度条,用来显示它的生命值。然后这里定义了两个发射子弹的时间计时器变量,这种变量一般在update中使用:根据deltaTime累加值来判断,如果大于这个发射间隔就发射子弹,然后重置成0。

    this.enemy1ShootTimer += deltaTime;

                    if (this.enemy1ShootTimer > this.enemy1ShootSpeed) {

                        this.enemy1Shoot(enemyBulletTypeEnum.BULLET01);

                        this.enemy1ShootTimer = 0;

                    }

deltaTime是一个跟unity里一样的变量,它的值根据每一个机器的帧数决定,如果1秒60帧的话,这个数值就是1/60=0.166667。

    public varHealth: number = 0;

    @property(ProgressBar)

    public healthBar: ProgressBar = null;

    enemy1ShootTimer: number = 0;   //敌机1发射子弹计时器

    enemy2ShootTimer: number = 0;   //敌机2发射子弹计时器

敌机生成子弹的函数:

  //生成子弹

    enemy1Shoot(enemybulletType: enemyBulletTypeEnum) {

        let posBegin: Vec3 = new Vec3();    //定义子弹开始的位置

        let enemyBullet: Node = null;

        enemyBullet = this.enemyBulletFactory.createProduct(enemybulletType);  //制作子弹

        if (this.node) {

            this.node.parent.addChild(enemyBullet);        //添加节点到画布

            this.curPos = this.node.getPosition();      //得到敌机机当前位置

            posBegin.x = this.curPos.x;

            posBegin.y = this.curPos.y - 50;        //设置到机头位置

            enemyBullet.setPosition(posBegin);

        }

    }

子弹的位移函数可以根据子弹类型来定义:比如下面的函数就是子弹在X轴方向上来回碰撞。

  // X轴方向来回撞着向下

    updateEnemy02(deltaTime: number) {

        this.curPos = this.node.getPosition();

        const selfWidth = this.node.getComponent(UITransform).contentSize.width

        this.curPos.y -= this.startSpeed * deltaTime;

        //一水平方向上移动

        if (this.isLeft) {

            this.curPos.x -= 200 * deltaTime;

            if (this.curPos.x < -(common.viewWidth / 2 - selfWidth / 2)) {

                this.isLeft = false;

            }

        } else {

            this.curPos.x += 200 * deltaTime;

            if (this.curPos.x > (common.viewWidth / 2 - selfWidth / 2)) {

                this.isLeft = true;

            }

        }

        this.node.setPosition(this.curPos);

        if (this.node.position.y < -common.viewHeight / 2) {

            this.recycleProduct(this.enemyType)

        }

    }

好了今天就先说这么多,对了记得来体验一下这款小游戏哦,打不过的话一定要升级装备!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3