木灵鱼儿
阅读:121
Cocos Creator 制作仿DNF血条优化版
前言
看本文章前建议先看上一篇《Cocos Creator 制作仿DNF血条》,这样就会明白优化的处理有哪些了。
上一章中,我们的血条它的逻辑是两条血条来回切换,包括颜色,宽度,层级,控制起来其实是非常不方便的, 逻辑也变得复杂了,而且没有考虑到秒杀的情况下血条的处理,所以这次根据同事提供的好主意我重新写了一版。
教程
原理
我们可以将第二条血条作为一个固定的底色来控制,只有当第二条血没有的时候,我们将其设置白色的,这样每次血条width控制其实都在第一条上面。
也就是说,血条的宽度控制其实一直是第一条在循环控制,扣完了马上切换成第二条的颜色,然后宽度设满,第二条的颜色变成第三条,直到没有血条了,第二条变成白色的就行了。
第二条血条宽度一直不变。
所以我们的层级结构就变得更加简单了。
三个子节点就完成了,一个bg底色,两个血条,two同时是血条,也是白色的底色。
代码
代码上就更加简单了,我想了下没必要拆分一个独立脚本来控制血条,直接给他们的父级节点boss_blood
挂载一个脚本即可。
import { _decorator, Component, Node, UITransform, Sprite, math } from "cc";
const { ccclass, property } = _decorator;
@ccclass("BossBlood")
export class BossBlood extends Component {
@property({ type: Node, displayName: "第一条血条" })
private one: Node;
@property({ type: Node, displayName: "第二条血条" })
private two: Node;
private maxWidth: number = 0;
private colors: Array<string> = ["#AB1A22", "#FA8600", "#92C702", "#007CC8"];
private activeColorIndex: number = 0;
private defaultColor: math.Color = math.color("#ffffff");
//计算
private total: number = 0; //总血条数
private rate: number = 1; //比例
private hp: number = 0; //总hp
private activeHp: number = 0; //当前hp
private activeIndex: number = 1; //当前第几条血条
onLoad() {
//获取ui和精灵组件
this.one["_mySprite"] = this.one.getComponent(Sprite);
this.one["_myUITransform"] = this.one.getComponent(UITransform);
this.two["_mySprite"] = this.two.getComponent(Sprite);
this.two["_myUITransform"] = this.two.getComponent(UITransform);
//获取血条宽度
this.maxWidth = this.one["_myUITransform"].width;
this.setBlood(2000, 500);
setInterval(() => {
this.buckleBlood(20);
}, 30);
}
/**
* @description: 设置血条
* @param {number} hp
* @param {number} max
* @Date: 2022-07-31 17:21:20
* @Author: mulingyuer
*/
public setBlood(hp: number, max: number) {
//重置血条
this.resetBlood();
//重置数据
this.total = Math.floor(hp / max);
this.rate = this.maxWidth / max;
this.hp = hp;
this.activeHp = hp;
this.activeColorIndex = 0;
//初次血条颜色
if (this.total === 1) {
this.one["_mySprite"].color = this.getNextColor();
this.one.active = true;
} else {
this.one["_mySprite"].color = this.getNextColor();
this.one.active = true;
this.two["_mySprite"].color = this.getNextColor();
this.two.active = true;
}
}
/**
* @description: 扣血
* @param {number} val
* @Date: 2022-07-31 17:35:28
* @Author: mulingyuer
*/
public buckleBlood(val: number): void {
if (this.activeHp <= 0) return;
//秒杀的情况
if (val >= this.activeHp) {
this.activeHp = 0;
this.activeColorIndex = this.total - 1;
this.activeIndex = this.total;
if (this.total === 1) {
this.one["_myUITransform"].width = 0;
} else {
this.two["_mySprite"].color = this.defaultColor;
this.one["_myUITransform"].width = 0;
this.one["_mySprite"].color = this.getNextColor();
}
return;
}
//指定血条扣除
let remainder: number = this.specBloodBuckle(this.one, val);
while (remainder > 0) {
//一条血不够扣
this.activeIndex += 1;
this.one["_mySprite"].color = this.two["_mySprite"].color;
this.one["_myUITransform"].width = this.maxWidth;
//第二条血颜色变成下一条的颜色或者没有了就变成默认色
if (this.activeIndex >= this.total) {
this.two["_mySprite"].color = this.defaultColor;
} else {
this.two["_mySprite"].color = this.getNextColor();
}
//继续扣第一条血
remainder = this.specBloodBuckle(this.one, remainder, true);
}
}
/** 指定血条扣血 */
private specBloodBuckle(blood: Node, val: number, isRemainder = false): number {
//扣除血量
if (!isRemainder) this.activeHp -= val;
if (this.activeHp < 0) this.activeHp = 0;
//控制血条
const activeWidth = blood["_myUITransform"].width;
const buckleVal = isRemainder ? val : val * this.rate;
const resultVal = activeWidth - buckleVal;
if (this.activeHp <= 0) {
//没血了
blood["_myUITransform"].width = 0;
return 0;
} else if (resultVal < 0) {
//一条血不够扣了
blood["_myUITransform"].width = 0;
return Math.abs(resultVal);
} else {
//正常扣血
blood["_myUITransform"].width = resultVal;
return 0;
}
}
/** 初始化血条 */
private resetBlood() {
const arr = [this.one, this.two];
arr.forEach((node) => {
node["_myUITransform"].width = this.maxWidth;
node["_mySprite"].color = this.defaultColor;
node.active = true;
});
}
/** 获取指定下标的血条颜色 */
private getNextColor(): math.Color {
if (this.activeColorIndex >= this.colors.length) {
this.activeColorIndex = 0;
}
return math.color(this.colors[this.activeColorIndex++]);
}
}
基本上就这样,当我们需要使用血条的时候,只需要通过boss_blood
节点,获取到它挂载的脚本BossBlood
组件,调用即可。
import { BossBlood } from "../blood/BossBlood";
const bossBloodScript = this.bossBlood.getComponent(BossBlood);
bossBloodScript.setBlood(2000, 500);
bossBloodScript.buckleBlood(50); //扣血操作
版权申明
本文系作者 @木灵鱼儿 原创发布在木灵鱼儿 - 有梦就能远航站点。未经许可,禁止转载。
相关推荐
Cocos Creator 制作仿DNF血条
前言最近在想boss的血条制作,比较简单的就是单条血条,但是我想弄个想类似dnf那种,血条减完一条还有一条的那种,于是有了本篇文章预览图教程节点结构由于cocos原生没有这种血条,我们需要手动创建,我先通过两个单色精灵模拟血条的框架,大概就是一个大的节点里面包含一个小的节点,两个节点颜色不一致,里面的节点宽高都小4px,这样就模拟出边框的效果了。然后就是血条了,我的原理是通过两个血条节点进行来回替换实现的,当血量有两管血的时候,一开始就扣除时控制第一条血条的width,然后这是这个计算非常重要,它需要return出余值,当它存在余值的时候,说明还能扣血, 此时一瞬间将第一条置于第二条的底...

Cocos Creator 事件穿透
前言Cocos Creator 中可以通过挂载 BlockInputEvents组件来防止事件穿透,但是如果我们想让它能够事件穿透,官方的文档只字不提。这个需求也是有场景的,比如我们有两个元素,A和B,B会浮动到A上,从而导致click被B给挡住了,从而A事件无法触发了,这种场景在打地鼠游戏中是常见的。解决办法我们需要给挡住的那个元素添加允许事件穿透,用上面的例子,那就是需要给B节点添加一个事件穿透。import { _decorator, Component, Node } from "cc"; const { ccclass, property } = _deco...
解决Cocos Creator单屏开发改动脚本文件后还需要切换到Cocos Creator以触发脚本编译
问题起源可能Cocos Creator软件的开发人员认为每个开发者都是有两个电脑屏幕的(苦笑);它的脚本检测逻辑是:当你改动完脚本文件后,切换到Cocos Creator面板,此时才会触发预览更新,如果你是双屏的话,就不用切换,一个屏幕打开Cocos Creator,一个屏幕打开vscode,然后vscode保存后就会自动更新,此时切换到浏览器预览,一切都是那么的正常,前提你得有两块屏幕。非常蛋疼!!!这真是一个糟糕的体验!如果我们是单屏,你vscode改动脚本保存后,还得切换到Cocos Creator软件上,然后再切到浏览器去查看,超恶心有没有,怎么会有这种体验,都2022年了,文件...
