Hello. I have a class I am using to create an object that extends from Phaser.Physics.Arcade.Sprite, called objectSpawner . I also have another class where the game is run called game. In game i create the objects. But I need to be able to call a function from objectSpawner within game, how do I do this.
Hi, you can instantiate the extended class in the game class and you can invoke methods on this instantiated member. Like;
let player = new ObjectSpawner();
player.walk();
or you can add a static method to the objectSpawner and use it like;
// in objectSpawner
class ObjectSpawner {
constructor() {
}
static getRandomColor() {
console.log("print some random color");
}
}
// in game
ObjectSpawner.getRandomColor();
on the other hand, if you want to have communication between two classes, so you can use Phaser’s events like;
class A {
constructor() {
}
init() {
scene.events.on("print", (param) => {
this.printText(param);
});
}
printText(text) {
console.log(text);
}
}
// in game
scene.events.emit("print", "HELLO");
What’s your code now?