Platformer Enemy Edge Detection
A sprite that uses an Arcade Physics sensor to detect the edges of platforms and automatically change direction.
1024 × 768
import ANIMATION_KEYS from "../animationKeys.js";
import SPRITE_KEYS from "../spriteKeys.js";
export class Cake extends Phaser.Physics.Arcade.Sprite
{
moveVelocity = 50;
direction = -1;
sensorSize = 10;
groundSensor;
groundOffset;
constructor(scene, x, y, direction = -1)
{
super(scene, x, y, SPRITE_KEYS.CAKE);
scene.add.existing(this);
scene.physics.add.existing(this);
this.setCollideWorldBounds(true);
this.setDepth(100);
this.anims.play(ANIMATION_KEYS.CAKE_WALK, true);
this.groundOffset = { x: this.body.halfWidth + (this.sensorSize * 0.5), y: this.body.halfHeight + (this.sensorSize * 0.5) };
this.groundSensor = scene.addSensor(x - this.groundOffset.x, y + this.groundOffset.y, this.sensorSize, this.sensorSize);
this.setDirection(direction);
}
preUpdate (time, delta)
{
super.preUpdate(time, delta);
if (this.direction === -1)
{
this.moveLeft();
}
else
{
this.moveRight();
}
}
setDirection (direction)
{
this.direction = direction || this.direction * -1;
const sensorPosition = { x: this.x + (this.direction * this.groundOffset.x), y: this.y + this.groundOffset.y };
this.groundSensor.body.reset(sensorPosition.x, sensorPosition.y);
}
moveLeft ()
{
const sensorPosition = { x: this.x - this.groundOffset.x, y: this.y + this.groundOffset.y };
this.setVelocityX(-this.moveVelocity);
this.groundSensor.setVelocityX(-this.moveVelocity);
this.flipX = true;
if (this.groundSensor.body.touching.none && !this.groundSensor.body.wasTouching.none)
{
this.setDirection();
}
}
moveRight ()
{
const sensorPosition = { x: this.x + this.groundOffset.x, y: this.y + this.groundOffset.y };
this.setVelocityX(this.moveVelocity);
this.groundSensor.setVelocityX(this.moveVelocity);
this.flipX = false;
if (this.groundSensor.body.touching.none && !this.groundSensor.body.wasTouching.none)
{
this.setDirection();
}
}
}More in GameObjects13 examples
Static Physics Sprite ClassDemonstrates creating a custom class that extends an Arcade Physics Sprite to define a static level exit with a specific texture frame and origin.Treasure Box Game ObjectA custom Arcade Physics Sprite class representing a treasure box that changes its animation frame and triggers a key spawn when activated.Animated Coin Collection EffectDemonstrates a custom Sprite class that manages its own spin animation, yoyo tween movement, sound effects, and self-destruction upon completion.Automated Sprite AI (Chick)A custom Arcade Physics Sprite class demonstrating automated movement, jumping on a timer, and collision-based direction switching.