class Example extends Phaser.Scene {
preload() {
this.load.image("pig", "https://assets.codepen.io/9367036/pig.png");
}
create() {
this.graphics = this.add.graphics();
this.graphics.lineStyle(2, 0xe6695b, 1);
this.centerX = config.width / 2;
this.centerY = config.height / 2;
this.points = [
new Phaser.Math.Vector2(this.centerX - 200, this.centerY),
new Phaser.Math.Vector2(this.centerX, this.centerY - 100),
new Phaser.Math.Vector2(this.centerX + 200, this.centerY)
];
this.angle = 0;
this.radius = 100;
// Create the spline for the follower
this.spline = new Phaser.Curves.Spline(this.points);
this.pig = this.add.follower(
this.spline,
this.points[0].x,
this.points[0].y,
"pig"
);
this.pig.setScale(0.5);
// Start the follower on the spline
this.pig.startFollow({
duration: 2000,
positionOnPath: true,
repeat: -1,
yoyo: true
//rotateToPath: true
});
}
update(time, delta) {
this.angle += delta * 0.001; // Increment angle for animation
// Update the position of the central point to make it move in a circle
this.spline.points[1].x = this.centerX + this.radius * Math.cos(this.angle);
this.spline.points[1].y = this.centerY + this.radius * Math.sin(this.angle);
// Clear and redraw the curve with the new position of the central point
this.graphics.clear();
this.graphics.lineStyle(2, 0xe6695b, 1);
this.spline.draw(this.graphics);
// Draw the anchor points
this.graphics.fillStyle(0xffffff, 1);
this.spline.points.forEach((point) => {
this.graphics.fillCircle(point.x, point.y, 4);
});
}
}
const config = {
type: Phaser.AUTO,
transparent: true,
width: 800,
height: 400,
scene: Example
};
const game = new Phaser.Game(config);