I’m getting a different behavior from physics.add.sprite and physics.add.existing
Existing is adding behind the background.
Why my sprite appears behind the background when I use
this.player = this.physics.add.existing(new Player(this, 20, 20, 'dude')) as Player
But appears in front of the background when I use
this.player = this.physics.add.sprite(20, 20, 'dude')
Both codes are literally on the same spot
Player class
import Phaser from 'phaser'
export default class Player extends Phaser.Physics.Arcade.Sprite {
constructor (
scene: Phaser.Scene,
x: number,
y: number,
texture: string,
frame?: string | number
) {
super(scene, x, y, texture, frame)
}
}
Issue Screen capture
physics.add.existing
is meant to take a GameObject that’s already been added to a scene and give it a physics body.
physics.add.sprite
adds a GameObject to a scene and also gives it a physics body.
Does that difference make sense?
If you want to look into it a little further, you can see the difference in the two method implementations:
physics.add.sprite
physics.add.existing
Thanks, it made sense. It doesn’t add to displayList and updateList.
This solved
import Phaser from 'phaser'
export default class Player extends Phaser.Physics.Arcade.Sprite {
constructor (
scene: Phaser.Scene,
x: number,
y: number,
texture: string,
frame?: string | number
) {
super(scene, x, y, texture, frame)
scene.physics.add.existing(this)
scene.sys.displayList.add(this)
scene.sys.updateList.add(this)
}
}
And now I can add to scene with this:
this.player = (new Player(this, 20, 20, 'dude'))