Confusion between Class Entities and single JS Phaser coding

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.

Only Phaser.Physics.Arcade.Image and Phaser.Physics.Arcade.Sprite have their own enableBody method.

1 Like

Thanks - good to know.

Is there a way that I could modify my Entities file to allow me to utilize that functionality? I’m still pretty new to a lot of this. I would have thought that since Entities is extending Phaser.GameObjects.Sprite it would ‘come along with it…’ but obviously not as it’s currently written.

Appreciate any tweaks / mods you can suggest. I’d really like to be able to still use it.

Instead of class Entity extends Phaser.GameObjects.Sprite you change it to extend the arcade class:
class Entity extends Phaser.Physics.Arcade.Sprite

1 Like

Thanks for your insight! Will definitely give it a whirl. Thanks again! =D

1 Like