Hi - I’ve been learning Phaser 3 for a few months now and really enjoy it. I made a fun little game as a single JS file and decided to convert it over to multi-JS files using SaucerInvaders ([Phaser 3] Tutorial: Saucer Invaders) and other similar multi-file JS Phaser games as a template.
I’ve run into a odd situation: in the single long file I can perform [GameObject].enableBody(true, x, y) without any problems. But when I try to do it when [GameObject] has been created inside the Entities file / class it suddenly responds with “[GameObject].enableBody is not a function.”
I’ve bypassed it with ‘work around’ of:
[GameObject].body.setEnable().reset(x, y);
[GameObject].setAlpha(1);
which, for my purposes, works close enough. I’d like to know how to correct or better write my Class extensions.
Currently my Entities.js file is structured as follows:
class Entity extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, key, type, frame) {
super(scene, x, y, key, frame);
this.scene = scene;
this.scene.add.existing(this);
this.scene.physics.world.enableBody(this, 0);
}
}
class Player extends Entity {
constructor(scene, x, y, key) {
super(scene, x, y, key, Player);
this.body.setCircle(25,5,20)
.setBounce(0.2)
.setGravityY(300)
.setCollideWorldBounds(true);
this.setDepth(10);
…
I currently have 7 sub-classes under Entities, but hopefully above is sufficient. Let me know if it’s not though.
Thanks & Appreciate any advice.