Fix: 修复火箭未爆炸bug(动态计算初速度确保必然到达目标高度)

This commit is contained in:
2026-02-27 15:38:21 +08:00
parent 9147dc0d01
commit c52998671b
+10 -5
View File
@@ -18,13 +18,19 @@ const FireworksEffect = (() => {
* @param {string} color 爆炸颜色
* @param {string} type 爆炸类型:sphere / willow / ring
*/
constructor(x, targetY, color, type) {
constructor(x, targetY, color, type, canvasHeight) {
this.x = x;
this.y = 9999; // 在 start() 里设置为 canvas.height
this.y = canvasHeight; // 从画布底部出发
this.targetY = targetY;
this.color = color;
this.type = type;
this.vy = -(12 + Math.random() * 4); // 上升速度
// 根据飞行距离动态计算初始速度,保证必然到达目标高度
// 等比级数求和:total = vy / (1 - 0.98) = vy × 50
// 加 10%~20% 余量,使火箭略微超过目标再触发爆炸(更真实)
const dist = canvasHeight - targetY;
this.vy = -(dist / 50) * (1.1 + Math.random() * 0.15);
this.trail = []; // 尾迹历史坐标
this.exploded = false;
this.done = false;
@@ -255,8 +261,7 @@ const FireworksEffect = (() => {
const ty = h * (0.08 + Math.random() * 0.45);
const color = COLORS[Math.floor(Math.random() * COLORS.length)];
const type = TYPES[Math.floor(Math.random() * TYPES.length)];
const rocket = new Rocket(x, ty, color, type);
rocket.y = h; // 从底部发射
const rocket = new Rocket(x, ty, color, type, h); // 传入画布高度
rockets.push(rocket);
launchCnt++;
}, 600);