From 9c07c52d34e0bdf0523b59de14f83fbbc2434009 Mon Sep 17 00:00:00 2001 From: AnRil Date: Mon, 1 Jun 2026 01:40:44 +0700 Subject: [PATCH] Fix moving-platform and enemy jitter at boundaries Moving platforms reversed velocity whenever |x-startX|>range, which stays true for several frames after the flip, so direction flipped every frame and the platform vibrated in place. Now reverse only when past the edge AND still heading outward. Same directional guard for enemy 'bug' patrol. Added a 6px deadzone to 'mev_bot' so it stops shaking when aligned with the player. Verified: a moving platform now flips ~4x over 360 frames and travels the full range symmetrically. --- src/entities/Enemy.js | 23 +++++++++++++++++------ src/entities/Platform.js | 12 +++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/entities/Enemy.js b/src/entities/Enemy.js index 4e3b91a..7a01c2c 100644 --- a/src/entities/Enemy.js +++ b/src/entities/Enemy.js @@ -47,9 +47,15 @@ export class Enemy extends Physics.Arcade.Sprite { if (this.enemyType === 'bug') { this.x += this.speed * dt; - if (Math.abs(this.x - this.startX) > this.patrolRange) { - this.speed *= -1; - this.setFlipX(this.speed < 0); + // Reverse only when past the patrol edge AND still heading outward, + // otherwise the flip re-triggers each frame and the bug jitters. + const dx = this.x - this.startX; + if (dx > this.patrolRange && this.speed > 0) { + this.speed = -Math.abs(this.speed); + this.setFlipX(true); + } else if (dx < -this.patrolRange && this.speed < 0) { + this.speed = Math.abs(this.speed); + this.setFlipX(false); } if (this.x < -60) this.x = GAME_WIDTH + 60; if (this.x > GAME_WIDTH + 60) this.x = -60; @@ -62,9 +68,14 @@ export class Enemy extends Physics.Arcade.Sprite { } else if (this.enemyType === 'mev_bot') { const player = this.scene.player; if (player && player.active) { - const dir = Math.sign(player.x - this.x); - this.x += dir * this.homeSpeed * dt; - this.setFlipX(dir < 0); + // Deadzone: stop chasing when roughly aligned so Math.sign doesn't + // flip every frame and make the bot vibrate around the player column. + const diff = player.x - this.x; + if (Math.abs(diff) > 6) { + const dir = Math.sign(diff); + this.x += dir * this.homeSpeed * dt; + this.setFlipX(dir < 0); + } } this.x = Phaser.Math.Clamp(this.x, 24, GAME_WIDTH - 24); const t = time - this.spawnTime; diff --git a/src/entities/Platform.js b/src/entities/Platform.js index ee0d7c6..41a8b5d 100644 --- a/src/entities/Platform.js +++ b/src/entities/Platform.js @@ -55,10 +55,16 @@ export class Platform extends Physics.Arcade.Sprite { preUpdate(time, delta) { super.preUpdate(time, delta); if (this.platformType === 'moving' && this.body) { - this.setVelocityX(this.moveSpeed); - if (Math.abs(this.x - this.startX) > this.moveRange) { - this.moveSpeed *= -1; + // Reverse direction only when past the edge AND still heading outward. + // (A plain |x-startX|>range flip re-triggers every frame at the boundary, + // which made the platform vibrate in place.) + const dx = this.x - this.startX; + if (dx > this.moveRange && this.moveSpeed > 0) { + this.moveSpeed = -Math.abs(this.moveSpeed); + } else if (dx < -this.moveRange && this.moveSpeed < 0) { + this.moveSpeed = Math.abs(this.moveSpeed); } + this.setVelocityX(this.moveSpeed); if (this.genesisGlow) this.genesisGlow.setPosition(this.x, this.y + 10); } }