Accessing player physics from another children class

I’m a bit rusty on JS classes , hope someone can help me out …

I hv world scene calling a Player class and Player.create() calling
this.scene.physics.add

How do I access the player Physics from world scene ??

I set the physics to this.currentPlayer and calling this.player.currentPlayer but it is not working…

What am I missing ??

I can’t do collision on world scene.

export default class world extends Phaser.Scene {
  constructor() {
    super({
      key: "world",
    });
  }

  init(data) {
    this.player = new Player(this, "world", {
      x: 300,
      y: 300,
      direction: "down",
    });
  }

 create() {
 
     this.player.create();

}

Player class

class Player {
  constructor(scene, room, position) {
    this.scene = scene;
    this.room = room;
    this.position = position;
    this.socket = io();
    this.playersObj = {};
    this.currentPlayer = {};
  }

  create() {

  addPlayer(id, x, y, direction) {
    console.log("*** addPlayer: ", id, x, y, direction);

    let key = "anna";
    this.playersObj[id] = this.scene.physics.add
      .sprite(x, y, key)
      .setScale(0.7);
    this.playersObj[id].anims.play("down");
    this.playersObj[id].anims.stop();
    this.currentPlayer = this.playersObj[id];

    console.log("currentPlayer: ", this.currentPlayer);
  }

player class from console.

player.currentPlayer is already ArcadeSprite.


1. Player {scene: world, room: 'world', position: {…}, socket: Socket, playersObj: {…}, …}

  1. currentPlayer: ArcadeSprite {_events: Events, _eventsCount: 2, scene: world, displayList: DisplayList, type: 'Sprite', …}
  2. playersObj: {ZqrLoRx3g8zbhp7GAAAj: ArcadeSprite}
  3. position: {x: 300, y: 300, direction: 'down'}
  4. room: "world"
  5. scene: world {sys: Systems, zoomFactor: 2, game: Game, anims: AnimationManager, cache: CacheManager, …}
  6. socket: Socket {io: Manager, nsp: '/', json: Socket, ids: 0, acks: {…}, …}
  7. [[Prototype]]: Object

Hey, I don’t know if it’s just a copy/paste mistake but in your Player class it says:

create() {

addPlayer(id, x, y, direction) {
    ....
}

So first of all there is a missing closing } bracket after create() {. And the this.scene.physics.add is never called because it’s in addPlayer, not create. Maybe this helps already :slight_smile:

Apologies, that was not the full codes, i just pasted relevant info only….

I added

class Player extends Phaser.GameObjects.Sprite

But collision is still not happening …

How do I troubleshoot when no collision happens ??

I’m sorry, but I don’t think we (or at least I) can’t help you without seeing the real code then, because it’s not clear what (or how) you’re trying to achieve (this). :confused:

I think the original Player class (no extends) was better.

In the scene you need to add a collider for it.

Turn on physics debug to confirm where the bodies are.

I call player.create(“robot”) at line 119

addPlayer at player.js at line 92

world.js world.js - Pastebin.com
player.js player.js - Pastebin.com

I think the original Player class (no extends ) was better.

Thanks… may I know why ?
I’m just trying everything … haha

In the scene you need to add a collider for it.
Turn on physics debug to confirm where the bodies are.
Yes, debug box is turn on …
Screenshot 2021-10-20 at 12.33.49 AM

I have 2 object this.player and I added this.player.currentPlayer
when I check this.player.x is always 0
when I check this.player.currentPlayer.x is the actual x position

this.player console

this.player.currentPlayer

Player should either extend Arcade.Sprite or it should create and store a sprite in currentPlayer, but not both.

What is playersObj for?

If you need to swap out sprites or use a Player instance across multiple scenes, then you should use no parent class and put the sprite in currentPlayer. But you have to give only currentPlayer to Phaser’s methods because it can’t do anything with the Player instance itself. And you have to use the sprite’s coordinates in player.currentPlayer.x etc.

If you don’t need any of that, then Player can extend Arcade.Sprite instead. Make sure the super arguments match.

If the sprites are moving the debug display should show little velocity rays.

What is playersObj for?

They are used to store all the other players return from socket.io
id is the socketio.id

the first entry of this.playersObj is the current player.

this.playersObj[id] = this.scene.physics.add
      .sprite(x, y, playerKey)
      .setScale(2);
    this.playersObj[id].anims.play(downKeyAvatar);
    this.playersObj[id].anims.stop();

If you need to swap out sprites or use a Player instance across multiple scenes, then you should use no parent class and put the sprite in currentPlayer . But you have to give only currentPlayer to Phaser’s methods because it can’t do anything with the Player instance itself. And you have to use the sprite’s coordinates in player.currentPlayer.x etc.

When I put this.currentPlayer = this.player.currentPlayer , I get undefined as during this.player.create(), the addPlayer() was not completed yet

Should I be using a promise to for this.player.create() ??

Is there a way to return the Arcade.Sprite from the this.player.create() ??
Then I can just do :

this.currentPlayer = this.player.create()

I would expect that to be handled in the scene instead.

You could return a Promise but the problem is scene update() won’t wait for that. You could add

function update () {
  if (!this.player.currentPlayer) return;
  // …
}

Another approach is

function update () {
  // …
  this.player.update(this.cursors);
}

I ended up doing all the collision at the player.js level where they were created.

Thanks