Problem with extended Phaser.Physics.Arcade.Sprite


#1

Hello I’m a beginner with Phaser and I want to extended a physcis sprite.

So this is my code.

class Player extends Phaser.Physics.Arcade.Sprite {
  constructor (scene, x, y,texture)
{
   super(scene, x, y,texture);
   this.setActive(true);	
  // this.setCollideWorldBounds(true);
 }
}
[...]
create: function ()
{
this.add.image(540,360, 'back');
player1 = new Player(this, 400, 100, 'dude');
this.add.existing(player1);
//player1 = this.physics.add.sprite(400,100, 'dude');    What I want to do with the class
}
});
var config = {
type: Phaser.AUTO,
width: 1080,
height: 720,
physics: {
	default: 'arcade',
	arcade: {
		gravity: { y: 1000 },
		debug: false
	}
},
scene: MyScene	
};
var game = new Phaser.Game(config);

The object display the sprite but the object don’t fall and I can not set something in my class.

I search a long time a long time on internet and I didn’t found.

Do someone know where is my mistake ?


#2

This is how it should look like:

class Player extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x, y, texture) {
    super(scene, x, y, texture)
    scene.add.existing(this)
    scene.physics.add.existing(this)

    this.setCollideWorldBounds(true)
  }
}

class MyScene extends Phaser.Scene {
  constructor() {
    super({ key: 'MyScene' })
  }

  create() {
    let player1 = new Player(this, 400, 100, 'dude')
  }
}

const config = {
  type: Phaser.AUTO,
  width: 1080,
  height: 720,
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 1000 },
      debug: false
    }
  },
  scene: [MyScene]
}
const game = new Phaser.Game(config)

Maybe my phaser-project-template-es6 will help you to get started with Phaser and ES6+ (ESNext)


#3

Add the following to the end of your constructor. There is an article Richard wrote somewhere about the Factories and how to extend them in order to be able to utilize them directly. I’m a newbie too, so my understanding of this is meh at best. BUT, if I remember correctly, the following lines of code are ran by the Phaser.Physics.Arcade.Sprite factory and are necessary for appying the sprite to the sprite manager and the physics to said sprite.

        scene.sys.displayList.add(this);
        scene.sys.updateList.add(this);
        scene.sys.arcadePhysics.world.enableBody(this, 0);

#4

scene.add.existing(this) will add the sprite to the displayList and updateList. You should use scene.add.existing(this) instead.

scene.physics.add.existing(this) will call world.enableBody(). You should use scene.physics.add.existing(this) instead.


#5

It looks like both options are perfectly viable. Why do you say “you should” use either or?

Reference: https://phaser.io/phaser3/devlog/130


#6

You’re right @Lindsey_Hensley ! Sorry about that. I should not have written ‘should’.
Based on the link you provided, you can safely use both methods :upside_down_face:


#7

Thank you for your awsers. I put the lines :

scene.add.existing(this);
scene.physics.add.existing(this);
this.setCollideWorldBounds(true);

inside my class and it’s work. Thank’s for your help.