cocos做飞机大战笔记【创建道具】
文章目录
- 效果
- 创建道具预制资源
- 创建道具材质
- 在引擎中调整道具并添加预制资源
- 编写脚本
- 添加碰撞类型与道具类型
- 子弹脚本
- 游戏管理脚本
- 将预制挂在脚本上并查看效果
- 完整代码
效果
道具运行轨迹大致是这样的
创建道具预制资源
创建道具材质
先按老规矩创建三个道具的材质
在引擎中调整道具并添加预制资源
然后添加碰撞体,首先在引擎添加道具的碰撞组,设置道具与玩家飞机碰撞
然后添加碰撞组件并且添加碰撞脚本
这一系列操作做完就可以设置成预制资源了
编写脚本
添加碰撞类型与道具类型
//Constant.ts/*** 碰撞类型*/public static CollisionType = {// 这里顺序对应面板设置碰撞分组的类型SELF_PLANE: 1 << 1,//玩家飞机类型ENEMY_PLANE: 1 << 2,//敌方飞机类型SELF_BULLET: 1 << 3,//玩家子弹类型ENEMY_BULLET: 1 << 4,//敌机子弹类型BULLET_PROP: 1 << 5,//道具碰撞类型};/*** 道具类型*/public static BulletPropType = {BULLET_M: 1,BULLET_H: 2,BULLET_S: 3,}
子弹脚本
这个脚本需要考虑
- 道具的创建与销毁
- 道具的移动
- 道具与玩家飞机碰撞后的效果
由于和之前碰撞写法思路差不多我这里直接粘代码了
//BulletProp.tsimport { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc';
import { Constant } from '../framework/Constant';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;@ccclass('BulletProp')
export class BulletProp extends Component {private _propSpeed = 0.3;//道具移动速度private _propXSpeed = 0.3;//X轴移动速度private _gameManager: GameManager = null;onEnable () {const collider = this.getComponent(Collider);collider.on('onTriggerEnter', this._onTriggerEnter, this);}onDisable () {const collider = this.getComponent(Collider);collider.off('onTriggerEnter', this._onTriggerEnter, this);}update (deltaTime: number) {let pos = this.node.position;//获取位置if (pos.x >= 15) {//到右边界处理this._propXSpeed = this._propSpeed;} else if (pos.x <= -15) {//到左边界处理this._propXSpeed = -this._propSpeed;}this.node.setPosition(pos.x + this._propXSpeed, pos.y, pos.z - this._propSpeed);pos = this.node.position;//超出50销毁if(pos.z > 50){this.node.destroy();}}//传速度与gameManagershow(gameManager: GameManager, speed: number){this._gameManager = gameManager;this._propSpeed = speed;}private _onTriggerEnter(event: ITriggerEvent){const name = event.selfCollider.node.name;// 根据名字替换子弹if(name === 'bulletH'){this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_H);} else if (name === 'bulletS') {this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_S);} else {this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_M);}this.node.destroy();}
}
游戏管理脚本
上面调用的changeBulletType不存在,此时要先添加这个方法及子弹的初始类型
private _bulletType = Constant.BulletPropType.BULLET_M;//子弹类型/*** 改变子弹类型* @param type 类型*/public changeBulletType(type: number){this._bulletType = type;}
道具是每10秒随机创建,这里就可以采用之前写好的定时器,改成一直创建就可以了
因为道具是每10秒出现一个,和这个飞机组合很像,即使超过3也没关系, 飞机大于20秒之后都是阶段三的时间段了保持就可以,所以让定时器一直执行不影响之前
- macro.REPEAT_FOREVER定时器一直执行
/*** 设计定时器 */private _changePlanMode() {/*** 每10秒改变一次状态,,根据状态就能决定采用什么组合*/this.schedule(this._modeChanged, 10, macro.REPEAT_FOREVER);//组件自带定时器(回调函数,间隔时间,重复次数,延迟时间)}private _modeChanged() {this._combinationInterval++this.createBulletProp();//创建子弹道具}
接下来就是创建子弹道具实现了
首先要定义道具的移动速度,以及道具的预制资源
然后随机创建道具
// prop 定义道具属性@property(Prefab)public bulletPropM: Prefab = null;@property(Prefab)public bulletPropH: Prefab = null;@property(Prefab)public bulletPropS: Prefab = null;@propertypublic bulletPropSpeed = 0.3;//道具速度/*** 创建子弹道具* 随机1~3是根据然后创建相对应的道具*/public createBulletProp(){const randomProp = math.randomRangeInt(1, 4);let prefab: Prefab = null;if(randomProp === Constant.BulletPropType.BULLET_H){prefab = this.bulletPropH;} else if(randomProp === Constant.BulletPropType.BULLET_H){prefab = this.bulletPropS;} else {prefab = this.bulletPropM;}//实例化道具const prop = instantiate(prefab);prop.setParent(this.node);prop.setPosition(15, 0, -50);const propComp = prop.getComponent(BulletProp);propComp.show(this, -this.bulletPropSpeed);}
将预制挂在脚本上并查看效果
完整代码
- Constant.ts
export class Constant{/*** 敌机类型*/public static EnemyType = {TYPE1:1,TYPE2:2,}/*** 组合类型*/public static Combination = {PLAN1:1,PLAN2:2,PLAN3:3,}/*** 碰撞类型*/public static CollisionType = {// 这里顺序对应面板设置碰撞分组的类型SELF_PLANE: 1 << 1,//玩家飞机类型ENEMY_PLANE: 1 << 2,//敌方飞机类型SELF_BULLET: 1 << 3,//玩家子弹类型ENEMY_BULLET: 1 << 4,//敌机子弹类型BULLET_PROP: 1 << 5,//道具碰撞类型};/*** 道具类型*/public static BulletPropType = {BULLET_M: 1,BULLET_H: 2,BULLET_S: 3,}
}
- BulletProp.ts
import { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc';
import { Constant } from '../framework/Constant';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;@ccclass('BulletProp')
export class BulletProp extends Component {private _propSpeed = 0.3;//道具移动速度private _propXSpeed = 0.3;//X轴移动速度private _gameManager: GameManager = null;onEnable () {const collider = this.getComponent(Collider);collider.on('onTriggerEnter', this._onTriggerEnter, this);}onDisable () {const collider = this.getComponent(Collider);collider.off('onTriggerEnter', this._onTriggerEnter, this);}update (deltaTime: number) {let pos = this.node.position;//获取位置if (pos.x >= 15) {//到右边界处理this._propXSpeed = this._propSpeed;} else if (pos.x <= -15) {//到左边界处理this._propXSpeed = -this._propSpeed;}this.node.setPosition(pos.x + this._propXSpeed, pos.y, pos.z - this._propSpeed);pos = this.node.position;//超出50销毁if(pos.z > 50){this.node.destroy();}}//传速度与gameManagershow(gameManager: GameManager, speed: number){this._gameManager = gameManager;this._propSpeed = speed;}private _onTriggerEnter(event: ITriggerEvent){const name = event.selfCollider.node.name;// 根据名字替换子弹if(name === 'bulletH'){this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_H);} else if (name === 'bulletS') {this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_S);} else {this._gameManager.changeBulletType(Constant.BulletPropType.BULLET_M);}this.node.destroy();}
}
- GameManager.ts
import { _decorator, Component, Node, Prefab, instantiate, math, Vec3, BoxCollider,macro } from 'cc';
import { Bullet } from '../bullet/Bullet';
import { BulletProp } from '../bullet/BulletProp';
import { EnemyPlane } from '../plane/EnemyPlane';
import { Constant } from './Constant';
const { ccclass, property } = _decorator;@ccclass('GameManager')
export class GameManager extends Component {// 关联玩家飞机@property(Node)public playerPlane: Node = null;// 关联所有子弹@property(Prefab)public bullet01: Prefab = null;@property(Prefab)public bullet02: Prefab = null;@property(Prefab)public bullet03: Prefab = null;@property(Prefab)public bullet04: Prefab = null;@property(Prefab)public bullet05: Prefab = null;// 射击周期@propertypublic shootTime = 0.3;// 子弹移动速度@propertypublic bulletSpeed = 1;/** 关联敌机 */@property(Prefab)public enemy01: Prefab = null;@property(Prefab)public enemy02: Prefab = null;@propertypublic createEnemtTime = 1;//创建敌机时间@propertypublic enemy1Speed = 0.5;//敌机1速度@propertypublic enemy2Speed = 0.7;//敌机2速度//子弹管理节点@property(Node)public bulletRoot: Node = null;// prop 定义道具属性@property(Prefab)public bulletPropM: Prefab = null;@property(Prefab)public bulletPropH: Prefab = null;@property(Prefab)public bulletPropS: Prefab = null;@propertypublic bulletPropSpeed = 0.3;//道具速度private _currShootTime = 0;// 是否触摸屏幕private _isShooting = false;private _currCreateEnemyTime = 0//当前创建的敌机时间private _combinationInterval = Constant.Combination.PLAN1//组合间隔状态private _bulletType = Constant.BulletPropType.BULLET_M;//子弹类型start() {this._init();}update(deltaTime: number) {// 这步加时间是为了发射子弹this._currShootTime += deltaTime;// 判断是触摸状态 并且射击时间大于我们的周期 发射子弹if (this._isShooting && this._currShootTime > this.shootTime) {this.createPlayerBullet();this._currShootTime = 0;}this._currCreateEnemyTime += deltaTime//判断组合方式if (this._combinationInterval == Constant.Combination.PLAN1) {//只创建单一的飞机 0-10秒飞机创建if (this._currCreateEnemyTime > this.createEnemtTime) {//普通飞机创建this.createEnemyPlane()this._currCreateEnemyTime = 0}} else if (this._combinationInterval == Constant.Combination.PLAN2) {// 10-20秒飞机创建if (this._currCreateEnemyTime > this.createEnemtTime * 0.9) {//0.9飞机组合间隔const randomCombination = math.randomRangeInt(1, 3)//随机1,2飞机if (randomCombination === Constant.Combination.PLAN2) {this.createCombination1()} else {this.createEnemyPlane()}this._currCreateEnemyTime = 0}} else {//20+ 飞机创建if (this._currCreateEnemyTime > this.createEnemtTime * 0.8) {//0.8飞机组合间隔const randomCombination = math.randomRangeInt(1, 4)//随机1,2,3飞机if (randomCombination === Constant.Combination.PLAN2) {this.createCombination1()} else if (randomCombination === Constant.Combination.PLAN3) {this.createCombination2()} else {this.createEnemyPlane()}this._currCreateEnemyTime = 0}}}/*** 加分*/public addScore() {}/*** 创建子弹*/public createPlayerBullet() {// 子弹实例化const bullet = instantiate(this.bullet01);// 将子弹放在子弹管理节点下面bullet.setParent(this.bulletRoot);// 获取飞机位置const pos = this.playerPlane.position;// 设置子弹位置bullet.setPosition(pos.x, pos.y, pos.z - 7);// 设置子弹速度const bulletComp = bullet.getComponent(Bullet);bulletComp.show(this.bulletSpeed, false)}/*** 创建敌机子弹* @param targetPos 敌机子弹位置*/public createEnemyBullet(targetPos: Vec3) {// 子弹实例化const bullet = instantiate(this.bullet01);// 将子弹放在子弹管理节点下面bullet.setParent(this.bulletRoot);// 设置子弹位置bullet.setPosition(targetPos.x, targetPos.y, targetPos.z + 6);// 设置子弹速度const bulletComp = bullet.getComponent(Bullet);bulletComp.show(1, true)/*** 敌机子弹分组*/const colliderComp = bullet.getComponent(BoxCollider);colliderComp.setGroup(Constant.CollisionType.ENEMY_BULLET);colliderComp.setMask(Constant.CollisionType.SELF_PLANE);//设置掩码}/*** 创建敌机**/public createEnemyPlane() {// 两架飞机随机选一(1,2)const whichEnemy = math.randomRangeInt(1, 3)let prefab: Prefab = nulllet speed = 0// 创建敌机1或2if (whichEnemy == Constant.EnemyType.TYPE1) {prefab = this.enemy01speed = this.enemy1Speed} else {prefab = this.enemy02speed = this.enemy2Speed}// 预制实例化const enemy = instantiate(prefab)console.log(enemy);enemy.setParent(this.node)const enemyComp = enemy.getComponent(EnemyPlane)enemyComp.show(this, speed, true)//单架敌机需要发射子弹// 设置飞机位置 const randomPos = math.randomRangeInt(-25, 26)enemy.setPosition(randomPos, 0, -50)}/*** 组合1创建 横向排列 z轴50,x轴从-20开始**/public createCombination1() {const enemyArray = new Array<Node>(5)//飞机数组for (let i = 0; i < enemyArray.length; i++) {// 敌机资源实例化enemyArray[i] = instantiate(this.enemy01)const element = enemyArray[i]element.parent = this.nodeelement.setPosition(-20 + i * 10, 0, -50)//-20起始左位置,10飞机间隔,其实创建位置//设置飞机速度const enemyComp = element.getComponent(EnemyPlane)enemyComp.show(this, this.enemy1Speed, false)//组合飞机不需要发射子弹}}/*** 组合2创建 V字排列**/public createCombination2() {const enemyArray = new Array<Node>(7)//飞机数组// 位置数组const combinationPos = [-21, 0, -60,-14, 0, -55,-7, 0, -50,0, 0, -45,7, 0, -50,14, 0, -55,21, 0, -60]for (let i = 0; i < enemyArray.length; i++) {// 敌机资源实例化enemyArray[i] = instantiate(this.enemy02)const element = enemyArray[i]element.parent = this.nodeconst startIndex = i * 3//因为位置数组有7个 但是位置有3×7 21个 所以每架飞机位置偏移是3element.setPosition(combinationPos[startIndex], combinationPos[startIndex + 1], combinationPos[startIndex + 2])//设置飞机速度const enemyComp = element.getComponent(EnemyPlane)enemyComp.show(this, this.enemy2Speed, false)//组合飞机不需要发射子弹}}/*** 创建子弹道具* 随机1~3是根据然后创建相对应的道具*/public createBulletProp(){const randomProp = math.randomRangeInt(1, 4);let prefab: Prefab = null;if(randomProp === Constant.BulletPropType.BULLET_H){prefab = this.bulletPropH;} else if(randomProp === Constant.BulletPropType.BULLET_H){prefab = this.bulletPropS;} else {prefab = this.bulletPropM;}//实例化道具const prop = instantiate(prefab);prop.setParent(this.node);prop.setPosition(15, 0, -50);const propComp = prop.getComponent(BulletProp);propComp.show(this, -this.bulletPropSpeed);}/*** 触摸状态设置* @param value true/false*/public isShooting(value: boolean) {this._isShooting = value;}/*** 改变子弹类型* @param type 类型*/public changeBulletType(type: number){this._bulletType = type;}/*** 默认发射子弹*/private _init() {this._currShootTime = this.shootTime;this._changePlanMode();}/*** 设计定时器 */private _changePlanMode() {/*** 每10秒改变一次状态,,根据状态就能决定采用什么组合*/this.schedule(this._modeChanged, 10, macro.REPEAT_FOREVER);//组件自带定时器(回调函数,间隔时间,重复次数,延迟时间)}private _modeChanged() {this._combinationInterval++this.createBulletProp();//创建子弹道具}
}
cocos做飞机大战笔记【创建道具】相关推荐
- cocos做飞机大战笔记【根据道具设置子弹】
文章目录 前言 定义子弹方向 定义子弹状态及根据状态移动 编写 M.H.S子弹创建逻辑 完整代码 前言 本文会介绍怎么根据拾取道具来改变子弹形态默认子弹是M s子弹 h子弹 定义子弹方向 首先可以想到 ...
- cocos做飞机大战笔记【添加游戏音效】
文章目录 前言 添加音频脚本并绑定组件 音频脚本 子弹发射播放音频并将播放音频方法在管理脚本中暴露 点击按钮播放音频 敌机销毁的时候播放音频 玩家飞机销毁播放音频 完整代码 前言 游戏音效会分为游戏开 ...
- cocos做飞机大战笔记【开始、游戏中,游戏结束界面】
文章目录 UI界面 创建开始前的场景 创建游戏中的界面 游戏结束的场景 脚本编写 1. 脚本中接受上面三个节点 2. 按钮绑定点击事件 3. 绑定游戏分数 3. 游戏结束逻辑 4. 设置血量 完整代码 ...
- cocos做飞机大战笔记【玩家飞机移动与子弹发射】
文章目录 滚动背景 创建脚本MovingSceneBg 让背景动起来 游戏开发常用框架 飞机移动+子弹发射 管理脚本文件夹 飞机移动 创建子弹材质 将子弹展示在编辑器中 绑定子弹脚本 创建子弹管理类b ...
- cocos做飞机大战笔记【敌机发射子弹】
文章目录 前言 敌机脚本 1. 初始变量 2. 子弹发射逻辑 3. 初始化敌机状态和子弹状态 子弹脚本 1.创建初始变量与飞机子弹类型判断逻辑 2. 状态初始化 游戏管理脚本 完整代码 GameMan ...
- 黑马程序员 飞机大战 笔记 上
飞机大战 前言 近来,受到朋友嘱托,做一款简单的小游戏,本小白很久没有碰过这方面的东西了,想知新还需温故,便书写此篇博客,本人只是利用博客记录自己的学习经历,水平较低. 很久之前,曾在b站观看黑马程序 ...
- Python实验,用pygame做飞机大战游戏设计
飞机大战游戏设计 摘 要:根据课程要求,以及面向对象程序设计的编程思想,在Windows操作系统环境下,运用PyCharm编译程序,以Python语言为开发语言,最终实现飞机大战游戏相应的游戏操作功能 ...
- windows程序设计——飞机大战笔记(单文档文件登陆界面)
//2015/07/21 /by xbw/// /环境 VS 2013 飞机大战做的差不多了,闲来无事加点高大上的东西,关于单文档的登陆界面::: 界面有点丑,但是足够账号登陆了,,先试一把: 还不错 ...
- python做飞机大战游戏_python实现飞机大战游戏
飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...
最新文章
- 46 万奖金等你拿 | 微众银行第二届金融科技高校技术大赛报名中
- angularjs中state的参数4_mpvue中使用Vuex
- linux weblogic10 安装,linux 静默安装weblogic10.36
- win7系统每次开机都需要疑难解答的原因与解决方法
- qt html导pdf 页眉,如何使用wkhtmltopdf unpatched qt在每个页面上添加页眉和页脚?
- 网页图表控件Highcharts如何详细设置参数
- 修改smb默认端口_centos7 ssh端口更改方法
- Swiper插件的基本使用方法和案例
- 鸿蒙系统当贝市场,鸿蒙os2.0系统怎么安装?ota即可!能与当贝d3x投影仪大屏玩?...
- MySQL 数据库导出
- 8.从Paxos到Zookeeper分布式一致性原理与实践---Zookeeper 运维
- make files touse cmd line to protect exe
- 16 数值的整数次方 (第3章 高质量的代码-代码的完整性)
- 硬件科普系列之硬盘——总线、协议、接口和固态硬盘篇
- 工商银行java script error windows7_Win8.1装工行网银提示"called runscript when not marked in progress"的解决方法...
- 神仙打架!传言阿里 P10 赵海平被 P11 多隆判定 3.25 离职,如何评价阿里 P10 赵海平对王垠的面试?...
- Techme INC:这5类人要警惕癌症!
- 玩游戏计算机虚拟内存怎么设置,多少虚拟内存设置适合玩游戏(多少虚拟内存设置适合)...
- FlutterWeb性能优化探索与实践
- 苹果x为什么总黑屏_苹果X手机为什么突然黑屏了
热门文章
- 红帽linux 系统日志,Linux系统日志的介绍
- STC15w4k32s 数字温度传感器 DS18B20 +Lcd1602
- 江苏省环保厅数据中心同城灾备建设项目
- python面试题百度云下载_【百度Python面试题及回答技巧有哪些?】-看准网
- 【SA8295P 源码分析】08 - XBL Loader 加载 SMSS、XBL Config、SHRM、CDT 、DDR、APDP、RamDump、OEM_MISC、AOP、QSEE过程分析
- 闲鱼神探——线上问题定位与快速解决
- 5,000名智利商家现接受加密货币支付
- 前端-js生成pdf文件
- 苏宁金融O2O,FinTech的第三路径
- 混频通信的matlab仿真,基于MATLAB的FHSS通信系统的设计与实现 PPT课件