Hello, new to this forum! I have a question regarding ES6 Class usage in Phaser.
So I have a Diver (sprite) class and the Main (state) class. I am using the two classes like this
class Diver extends Phaser.Sprite {
constructor(game, coordinateX, coordinateY, key) {
super(game, coordinateX, coordinateY, key);
this.game.add.sprite(coordinateX, coordinateY, key);
}
}
export default Diver;
import Diver from "../objects/Diver";
class Main extends Phaser.State {
init() {}
preload() {
this.game.load.image("diver", "/public/images/diver.png");
}
create() {
const diver = new Diver(this.game, 200, 200, "diver");
}
update() {}
}
export default Main;
Is this approach above the best way to do this? Seems that it is not as encapsulated as I thought it will be.
1. What if I want to put the `game.load.image` inside the Diver class?
2. I am adding the Diver to the Main by using `this.game.add.sprite` method inside the Diver's constructor. Seems odd to me or it isn't? What if I want to do this inside Main class and not inside Diver class?
Thanks for the help!