Hello, how are you, I’m learning phaser, and I have a little problem.
I’m making a train that goes through some tracks, the problem is when this train has to turn, I can’t make it turn smoothly or in the right direction.
With the code I show now, it does it smoothly but in the wrong direction.
import MoveTo from "../../node_modules/phaser3-rex-plugins/plugins/moveto.js";
var path;
var rails;
var ROTATION_SPEED = 1 * Math.PI;
class Game extends Phaser.Scene {
constructor() {
super("game");
}
preload() {}
create() {
this.map = this.add.image(0, 0, "mapa1");
const map = this.make.tilemap({ key: "map" });
const tileset = map.addTilesetImage("texture", "tiles");
map.createLayer("Rails", tileset);
const objects = map.createFromObjects("ruote");
/* path = { t: 0, vec: new Phaser.Math.Vector2() }; */
rails = objects.map((rail, i) => new Phaser.Math.Vector2(rail.x, rail.y));
/* curve = new Phaser.Curves.Spline(
objects.map((rail) => new Phaser.Math.Vector2(rail.x, rail.y))
); */
this.train = this.physics.add.sprite(rails[0].x, rails[0].y, "train");
this.path = 0;
this.moveTo = this.plugins
.get("rexMoveTo")
.add(this.train, {
speed: 400,
})
.on("complete", () => {
this.path++;
if (this.path < rails.length) {
this.moveTo.moveTo(rails[this.path].x, rails[this.path].y);
this.rotation = Phaser.Math.Angle.BetweenPoints(
this.train,
rails[this.path]
);
}
});
this.moveTo.moveTo(rails[0].x, rails[0].y);
this.cameras.main.startFollow(this.train, false);
/*
this.tween = this.tweens.add({
targets: path,
t: 1,
ease: "Linear",
duration: 10000,
repeat: -1,
}); */
}
update(time, delta) {
this.train.rotation = Phaser.Math.Angle.RotateTo(
this.train.rotation,
this.rotation,
ROTATION_SPEED * 0.001 * delta
);
}
/* update() {
console.log(this.tween.isPaused());
if (this.tween.isPaused() === false) {
curve.getPoint(path.t, path.vec);
this.train.rotation =
Phaser.Math.Angle.BetweenPoints(path.vec, this.train) + 92.68;
this.train.setPosition(path.vec.x, path.vec.y);
}
} */
render() {}
}
export default Game;