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.
This commit is contained in:
2026-06-01 01:40:44 +07:00
parent 22dec51e93
commit 9c07c52d34
2 changed files with 26 additions and 9 deletions

View File

@@ -47,9 +47,15 @@ export class Enemy extends Physics.Arcade.Sprite {
if (this.enemyType === 'bug') { if (this.enemyType === 'bug') {
this.x += this.speed * dt; this.x += this.speed * dt;
if (Math.abs(this.x - this.startX) > this.patrolRange) { // Reverse only when past the patrol edge AND still heading outward,
this.speed *= -1; // otherwise the flip re-triggers each frame and the bug jitters.
this.setFlipX(this.speed < 0); 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 < -60) this.x = GAME_WIDTH + 60;
if (this.x > GAME_WIDTH + 60) this.x = -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') { } else if (this.enemyType === 'mev_bot') {
const player = this.scene.player; const player = this.scene.player;
if (player && player.active) { if (player && player.active) {
const dir = Math.sign(player.x - this.x); // Deadzone: stop chasing when roughly aligned so Math.sign doesn't
this.x += dir * this.homeSpeed * dt; // flip every frame and make the bot vibrate around the player column.
this.setFlipX(dir < 0); 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); this.x = Phaser.Math.Clamp(this.x, 24, GAME_WIDTH - 24);
const t = time - this.spawnTime; const t = time - this.spawnTime;

View File

@@ -55,10 +55,16 @@ export class Platform extends Physics.Arcade.Sprite {
preUpdate(time, delta) { preUpdate(time, delta) {
super.preUpdate(time, delta); super.preUpdate(time, delta);
if (this.platformType === 'moving' && this.body) { if (this.platformType === 'moving' && this.body) {
this.setVelocityX(this.moveSpeed); // Reverse direction only when past the edge AND still heading outward.
if (Math.abs(this.x - this.startX) > this.moveRange) { // (A plain |x-startX|>range flip re-triggers every frame at the boundary,
this.moveSpeed *= -1; // 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); if (this.genesisGlow) this.genesisGlow.setPosition(this.x, this.y + 10);
} }
} }