Physics.add.existing not work correctly

Hi forum.
I faced a problem with Phaser v.3.24.1.

When I adds Sprite to scene via “this.physics.add.existing” func, it become not visible.

Here an example below:

class Entity extends Phaser.GameObjects.Sprite {
constructor(scene: Phaser.Scene, x: number, y: number, texture: string | Phaser.Textures.Texture) {
super(scene, x, y, texture);
}
}

export default class TestScene extends Phaser.Scene {
constructor() {
super({ key: ‘TestScene’ });
}

preload() {
    this.load.image('aim', 'assets/img/aim.png');
}

create() {
    // this sprite isn't visible
    this.physics.add.existing(new Entity(this, 300, 400, 'aim'));
    // this sprite isn't visible
    this.physics.add.existing(new Phaser.GameObjects.Sprite(this, 900, 400, 'aim'));

    // this sprite works well
    this.physics.add.sprite(1500, 400, 'aim');
}

}

What I’m doing wrong?

:wave:

You need to use scene.add.existing(…) (in the constructor) or this.add.existing(…) (in create) also.

physics.add.existing(…) only adds a physics body and adds it to the physics simulation.

2 Likes

Thanks!