点击上方亿元程序员+关注和★星标
根本跑不了
引言
实现导弹跟随的游戏开发技巧
在游戏开发中,实现导弹跟随是一项常见而关键的技术。
下面我们将通过分析一段游戏代码,深入了解如何在游戏中实现导弹跟随的效果。
本文源工程在文末获取,小伙伴们自行前往。
直接上代码
import { _decorator, CCFloat, Component, EventTouch, input, Input, instantiate, Node, Sprite, tween, UITransform, v3, Vec2, Vec3, view, Widget } from 'cc';const { ccclass, property } = _decorator;@ccclass('BulletFollow2D')export class BulletFollow2D extends Component { @property(Node) target: Node; @property(Node) bulletPfb: Node; @property(Node) bumpPfb: Node; @property(CCFloat) internal: number = 5; @property(CCFloat) speed: number = 100; touchDown: boolean = false; bullets: Node[] = []; tick: number = this.internal; start() { input.on(Input.EventType.TOUCH_START, (event: EventTouch) => { this.onTouch(event); this.touchDown = true; }, this); input.on(Input.EventType.TOUCH_MOVE, (event: EventTouch) => { if (this.touchDown) this.onTouch(event); }, this); input.on(Input.EventType.TOUCH_END, (event: EventTouch) => { this.touchDown = false; }, this); } onTouch(event: EventTouch) { var size = view.getVisibleSize(); this.target.setPosition(event.getUILocation().x - size.width / 2, event.getUILocation().y - size.height / 2, 0); } update(deltaTime: number) { this.tick += deltaTime; if (this.tick >= this.internal) { this.tick -= this.internal; var bullet = instantiate(this.bulletPfb); bullet.getComponent(Widget).destroy(); bullet.scale = this.bulletPfb.scale.clone().divide3f(2, 2, 2); bullet.parent = this.bulletPfb.parent; tween(bullet).by(1, { position: v3(0, this.bulletPfb.getComponent(UITransform).width / 2 * this.bulletPfb.scale.x + bullet.getComponent(UITransform).width / 2 * bullet.scale.x, 0) }) .call(() => { this.bullets.push(bullet); }).start(); } for (let i = this.bullets.length - 1; i >= 0; i--) { let bullet = this.bullets[i]; let position = bullet.position; let targetPos = this.target.getPosition(); let dir = targetPos.subtract(position).normalize(); bullet.angle = Math.atan2(-dir.y, -dir.x) * 180 / Math.PI; bullet.setPosition(position.x + dir.x * this.speed * deltaTime, position.y + dir.y * this.speed * deltaTime, position.z); if (Vec3.distance(bullet.position, this.target.position) < 10) { this.bullets.splice(i, 1); bullet.destroy(); let bump = instantiate(this.bumpPfb); bump.parent = this.bumpPfb.parent; bump.position = this.target.position; bump.scale = Vec3.ZERO; bump.active = true; tween(bump) .to(0.3, { scale: v3(0.2, 0.2, 0.2) }) .delay(0.1) .call(() => { bump.destroy(); }) .start(); } } }}
代码解析
让我们逐步解析代码:
属性:
target: 导弹跟随的目标节点。
bulletPfb: 导弹的预制节点。
bumpPfb: 撞击效果的预制节点。
internal: 导弹生成的间隔时间。
speed: 导弹移动的速度。
变量:
touchDown: 触摸是否按下的标志。
bullets: 保存导弹节点的数组。
tick: 计时器,用于控制导弹生成的时间间隔。
start方法:
处理触摸事件,监听触摸开始、移动和结束。
onTouch方法:
根据触摸位置设置目标节点的位置。
update方法:
处理导弹的生成和跟随逻辑。
控制导弹生成的间隔时间。
计算导弹的角度和位置,使其朝向目标节点移动。
如果导弹接近目标节点,触发撞击效果并销毁导弹。
实现关键技巧
触摸事件处理:
通过监听触摸开始、移动和结束事件,实时更新目标节点的位置,使其跟随玩家的触摸移动。
导弹生成:
根据设定的时间间隔,生成新的导弹节点,并设置其初始位置和缩放。
导弹跟随:
在每帧更新中,计算导弹朝向目标节点的方向,并根据速度移动导弹。
撞击效果:
当导弹接近目标节点时,触发撞击效果,创建撞击效果节点并播放动画,最终销毁效果节点。
优化建议
对象池管理:
使用对象池管理导弹节点,以便重复使用,提高性能。
动画系统:
利用游戏引擎的动画系统,更灵活地实现导弹和撞击效果的动画。
参数调优:
根据实际需求调整导弹生成间隔、速度、撞击距离等参数,以达到最佳的游戏体验。
效果演示
我像只鱼儿在你的荷塘
结语
通过以上技巧和优化建议,你可以更好地理解和运用导弹跟随的实现方式,使游戏开发更加高效和有趣。
本文源工程可通过私信BulletFollow2D获取。
我是"亿元程序员",一位有着8年游戏行业经验的主程。在游戏开发中,希望能给到您帮助, 也希望通过您能帮助到大家。
AD:笔者线上的小游戏《贪吃蛇掌机经典》《重力迷宫球》《填色之旅》大家可以自行点击搜索体验。
实不相瞒,想要个赞和在看!请把该文章分享给你觉得有需要的其他小伙伴。谢谢!
推荐专栏:
100个Cocos实例
8年主程手把手打造Cocos独立游戏开发框架
和8年游戏主程一起学习设计模式
从零开始开发贪吃蛇小游戏到上线系列
知识付费专栏
点击下方绿色按钮+关注。