cocos做飞机大战笔记【玩家飞机移动与子弹发射】
文章目录
- 滚动背景
- 创建脚本MovingSceneBg
- 让背景动起来
- 游戏开发常用框架
- 飞机移动+子弹发射
- 管理脚本文件夹
- 飞机移动
- 创建子弹材质
- 将子弹展示在编辑器中
- 绑定子弹脚本
- 创建子弹管理类bulletManager
- 创建游戏管理类GameManager
滚动背景
思路是两个图片拼接移动
创建脚本MovingSceneBg
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;/*** Predefined variables* Name = MovingSceneBg* DateTime = Mon Apr 04 2022 16:57:20 GMT+0800 (中国标准时间)* Author = YKL970719* FileBasename = MovingSceneBg.ts* FileBasenameNoExtension = MovingSceneBg* URL = db://assets/script/MovingSceneBg.ts* ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/**/@ccclass('MovingSceneBg')
export class MovingSceneBg extends Component {@property(Node)//图片1bg1: Node = null@property(Node)//图片2bg2: Node = null@property// 移动速度private _bgSpeed = 10@property//移动上限private _bgMovingRange = 90start() {}update(deltaTime: number) {this._moveBackground(deltaTime)}/*** 重置两个背景图片位置*/private _init() {this.bg1.setPosition(0, 0, 0)this.bg2.setPosition(0, 0, -this._bgMovingRange)}/*** 背景移动部分* @param deltaTime update中的帧*/private _moveBackground(deltaTime: number) {this.bg1.setPosition(0, 0, this.bg1.position.z + this._bgSpeed * deltaTime);this.bg2.setPosition(0, 0, this.bg2.position.z + this._bgSpeed * deltaTime);// 背景衔接部分if (this.bg1.position.z >= this._bgMovingRange) {this.bg1.setPosition(0, 0, this.bg2.position.z - this._bgMovingRange);}else if (this.bg2.position.z >= this._bgMovingRange) {this.bg2.setPosition(0, 0, this.bg1.position.z - this._bgMovingRange);}}
}
让背景动起来
调整相机为正交相机,然后拼接两张图片
如果不是很懂怎能创建的参考这个链接
拖入脚本
游戏开发常用框架
- gameManager 为游戏入口,负责游戏流程运作
- funcManager(为自己管理游戏类的名称)其他功能有必要的话都可以创建一个管理类,用来管理其他对象
- uiMain(或uiManager)专门用来处理用户输入以及它下面的界面
左部分为一些辅助功能,如果不同模块需要交互可以通过事件传递
- constant 存放游戏类型,供所有模块使用
飞机移动+子弹发射
管理脚本文件夹
按照上面所说的架构进行分类来创建文件夹
其中
- bullet用来存放子弹
- framework用来存放上面框架图中右边的逻辑
- plane用来存放飞机
- ui用来存放ui
飞机移动
在UIMain.ts中
import { _decorator, Component, Node, systemEvent, SystemEvent, Touch, EventTouch, Vec2 } from 'cc';
const { ccclass, property } = _decorator;/*** Predefined variables* Name = UIMain* DateTime = Mon Nov 15 2021 14:10:01 GMT+0800 (China Standard Time)* Author = mywayday* FileBasename = UIMain.ts* FileBasenameNoExtension = UIMain* URL = db://assets/script/ui/UIMain.ts* ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/**/@ccclass('UIMain')
export class UIMain extends Component {@propertypublic planeSpeed = 1;//飞机移动速度@property(Node)public playerPlane: Node = null;start () {this.node.on(SystemEvent.EventType.TOUCH_START, this._touchStart, this);this.node.on(SystemEvent.EventType.TOUCH_MOVE, this._touchMove, this);this.node.on(SystemEvent.EventType.TOUCH_END, this._touchEnd, this);}// update (deltaTime: number) {// // [4]// }_touchStart(touch: Touch, event: EventTouch){}_touchMove(touch: Touch, event: EventTouch){//获取当前触点值与上一次触点值之间的差值 const delta = touch.getDelta();//获取当前位置let pos = this.playerPlane.position;//移动位置z与x移动 y轴不变 /*** 乘0.01是因为每移动一段距离移动的都是单位,但是在操作屏幕触点的时候操作的其实是像素,两者之间不能转换,所以只能预估一下大概的移动距离 */this.playerPlane.setPosition(pos.x + 0.01 * this.planeSpeed * delta.x, pos.y, pos.z - 0.01 * this.planeSpeed * delta.y);}_touchEnd(touch: Touch, event: EventTouch){}
}*/
然后运行查看效果飞机就能拖动了
创建子弹材质
将子弹展示在编辑器中
创建空节点bullet01并在其下创建quad重命名为body
创建quad操作步骤:鼠标右键->3D对象->Quad
替换材质并调整位置
绑定子弹脚本
//Bullet.ts
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;/*** Predefined variables* Name = Bullet* DateTime = Mon Nov 15 2021 14:58:43 GMT+0800 (China Standard Time)* Author = mywayday* FileBasename = Bullet.ts* FileBasenameNoExtension = Bullet* URL = db://assets/script/bullet/Bullet.ts* ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/**/// 当前移动的最大位置范围为50
const OUTOFRANGE = 50;@ccclass('Bullet')
export class Bullet extends Component {// 子弹速度@propertypublic bulletSpeed = 0;start () {// [3]}update (deltaTime: number) {// 子弹移动const pos = this.node.position;const moveLength = pos.z - this.bulletSpeed;this.node.setPosition(pos.x, pos.y, moveLength);// 超出屏幕销毁if(moveLength > OUTOFRANGE){this.node.destroy();console.log('bullet destroy');}}
}
将子弹绑定脚本并设置成预制资源体
创建子弹管理类bulletManager
创建游戏管理类GameManager
- 脚本GameManager
import { _decorator, Component, Node, Prefab, instantiate } from 'cc';
import { Bullet } from '../bullet/Bullet';
const { ccclass, property } = _decorator;/*** Predefined variables* Name = GameManager* DateTime = Mon Nov 15 2021 16:15:32 GMT+0800 (China Standard Time)* Author = mywayday* FileBasename = GameManager.ts* FileBasenameNoExtension = GameManager* URL = db://assets/script/framework/GameManager.ts* ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/**/@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(Node)public bulletRoot: Node = null;private _currShootTime = 0;// 是否触摸屏幕private _isShooting = false;start () {this._init();}update (deltaTime: number) {// 这步加时间是为了发射子弹this._currShootTime += deltaTime;// 判断是触摸状态 并且射击时间大于我们的周期 发射子弹if(this._isShooting && this._currShootTime > this.shootTime){this.createPlayerBullet();this._currShootTime = 0;}}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.bulletSpeed = this.bulletSpeed;}/*** 触摸状态设置* @param value true/false*/public isShooting(value: boolean){this._isShooting = value;}/*** 默认发射子弹*/private _init(){this._currShootTime = this.shootTime;}
}
- 脚本UIMain
import { _decorator, Component, Node, systemEvent, SystemEvent, Touch, EventTouch, Vec2 } from 'cc';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;/*** Predefined variables* Name = UIMain* DateTime = Mon Nov 15 2021 14:10:01 GMT+0800 (China Standard Time)* Author = mywayday* FileBasename = UIMain.ts* FileBasenameNoExtension = UIMain* URL = db://assets/script/ui/UIMain.ts* ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/**/@ccclass('UIMain')
export class UIMain extends Component {@propertypublic planeSpeed = 1;//飞机移动速度@property(Node)public playerPlane: Node = null;//gameManager的引用@property(GameManager)public gameManager: GameManager = null;start() {this.node.on(SystemEvent.EventType.TOUCH_START, this._touchStart, this);this.node.on(SystemEvent.EventType.TOUCH_MOVE, this._touchMove, this);this.node.on(SystemEvent.EventType.TOUCH_END, this._touchEnd, this);}// update (deltaTime: number) {// // [4]// }/*** 触摸开始 子弹发射* @param touch * @param event */_touchStart(touch: Touch, event: EventTouch) {this.gameManager.isShooting(true);}_touchMove(touch: Touch, event: EventTouch) {//获取当前触点值与上一次触点值之间的差值 const delta = touch.getDelta();//获取当前位置let pos = this.playerPlane.position;//移动位置z与x移动 y轴不变 /*** 乘0.01是因为每移动一段距离移动的都是单位,但是在操作屏幕触点的时候操作的其实是像素,两者之间不能转换,所以只能预估一下大概的移动距离 */this.playerPlane.setPosition(pos.x + 0.01 * this.planeSpeed * delta.x, pos.y, pos.z - 0.01 * this.planeSpeed * delta.y);}/*** 触摸结束 子弹取消发射* @param touch * @param event */_touchEnd(touch: Touch, event: EventTouch) {this.gameManager.isShooting(false);}
}
编辑器关联对应的资源与节点
然后运行游戏测试就可以看见效果啦
cocos做飞机大战笔记【玩家飞机移动与子弹发射】相关推荐
- cocos做飞机大战笔记【根据道具设置子弹】
文章目录 前言 定义子弹方向 定义子弹状态及根据状态移动 编写 M.H.S子弹创建逻辑 完整代码 前言 本文会介绍怎么根据拾取道具来改变子弹形态默认子弹是M s子弹 h子弹 定义子弹方向 首先可以想到 ...
- Unity飞机大战(1) 玩家飞机移动控制和子弹发射+生成陨石和敌机
Unity制作飞机大战第一步 1.把资源包导入Unity并搭建好场景 2.并把做好的预制体 拖入一个新的文件夹 如下图: 飞机控制脚本,此脚本挂在玩家飞机上 using System.Collecti ...
- 飞机大战-控制玩家飞机左右移动
# -*- coding:utf-8 -*-import pygame import timedef main():#1. 创建窗口screen = pygame.display.set_mode(( ...
- 飞机大战--显示玩家飞机
# -*- coding:utf-8 -*-import pygame import timedef main():#1. 创建窗口screen = pygame.display.set_mode(( ...
- cocos做飞机大战笔记【添加游戏音效】
文章目录 前言 添加音频脚本并绑定组件 音频脚本 子弹发射播放音频并将播放音频方法在管理脚本中暴露 点击按钮播放音频 敌机销毁的时候播放音频 玩家飞机销毁播放音频 完整代码 前言 游戏音效会分为游戏开 ...
- cocos做飞机大战笔记【创建道具】
文章目录 效果 创建道具预制资源 创建道具材质 在引擎中调整道具并添加预制资源 编写脚本 添加碰撞类型与道具类型 子弹脚本 游戏管理脚本 将预制挂在脚本上并查看效果 完整代码 效果 道具运行轨迹大致是 ...
- cocos做飞机大战笔记【开始、游戏中,游戏结束界面】
文章目录 UI界面 创建开始前的场景 创建游戏中的界面 游戏结束的场景 脚本编写 1. 脚本中接受上面三个节点 2. 按钮绑定点击事件 3. 绑定游戏分数 3. 游戏结束逻辑 4. 设置血量 完整代码 ...
- cocos做飞机大战笔记【敌机发射子弹】
文章目录 前言 敌机脚本 1. 初始变量 2. 子弹发射逻辑 3. 初始化敌机状态和子弹状态 子弹脚本 1.创建初始变量与飞机子弹类型判断逻辑 2. 状态初始化 游戏管理脚本 完整代码 GameMan ...
- 黑马程序员 飞机大战 笔记 上
飞机大战 前言 近来,受到朋友嘱托,做一款简单的小游戏,本小白很久没有碰过这方面的东西了,想知新还需温故,便书写此篇博客,本人只是利用博客记录自己的学习经历,水平较低. 很久之前,曾在b站观看黑马程序 ...
最新文章
- linux多线程编写哲学家,Linux系统编程(三) ------ 多线程编程
- CSS3边框图片-像素虚边的问题
- ML Backpropagation算法实现的过程举例
- 如何在tomcat下应用部署日志_教妹子用IDEA创建web应用,部署到Tomcat服务器
- 记一次tomcat故障排查(转)
- (美国)数字设备公司 DEC
- linux宝塔怎么添加二级域名,如何绑定二级域名使用宝塔面板?
- WEBI上取月的整周
- 计算机组成原理试题和答案,计算机组成原理试题(含答案)
- 由博客评论引发的思考和实践(关于搜狗输入法)
- 【Kubernetes 企业项目实战】05、基于云原生分布式存储 Ceph 实现 K8s 数据持久化(下)
- win10 可以复制但无法粘贴的问题
- 10.Java面向对象进阶2
- 新数据整合的五大方式
- 上班族程序员必备的学习网站大全
- delete和delete[]
- 神经网络常用激活函数及其应用举例
- 使用SpringBoot+Vue+快递100API搭建一个快递查询网站
- 3.Ray-Event编写
- JS实现身份证的验证
热门文章
- 【jprofiler】jprofiler安装使用教程
- canvas绘制虚线图表
- Android日期时间与时区使用总结汇总
- 2014 360校园招聘技术类面试题
- tkinter使用canvas实现渐变色
- Python版股市情感分析源代码,提取投资者情绪,为决策提供参考
- 条码软件(Barcode Software)的类别、常用的条码软件、条码扫描软件经验分享
- 验证用户名重复注册PHP
- Fastdfs数据迁移方案
- 查询时报错The error may involve defaultParameterMap ### The error occurred while setting parameters