# Accessing player physics from another children class

**URL:** <https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474>\
**Category:** Phaser 3\
**Created:** [October 19, 2021, 12:42am UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474 "2021-10-19T00:42:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 12:42am UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/1 "2021-10-19T00:42:59Z")

</div>

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.

```javascript
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**

```javascript
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);
  }

```

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 12:46am UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/2 "2021-10-19T00:46:51Z")

</div>

player class from console.

player.currentPlayer is already ArcadeSprite.

```javascript

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

```

---

<div class="post-metadata">

**Author:** ![NickHatBoecker](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/nickhatboecker/32/5380_2.png) [@NickHatBoecker](https://phaser.discourse.group/u/NickHatBoecker)\
**Post date:** [October 19, 2021, 8:11am UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/3 "2021-10-19T08:11:27Z")

</div>

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

```javascript
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 🙂

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 3:03pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/4 "2021-10-19T15:03:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 3:09pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/5 "2021-10-19T15:09:08Z")

</div>

I added

`class Player extends Phaser.GameObjects.Sprite`

But collision is still not happening …

How do I troubleshoot when no collision happens ??

---

<div class="post-metadata">

**Author:** ![NickHatBoecker](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/nickhatboecker/32/5380_2.png) [@NickHatBoecker](https://phaser.discourse.group/u/NickHatBoecker)\
**Post date:** [October 19, 2021, 3:17pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/6 "2021-10-19T15:17:23Z")

</div>

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). 😕

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [October 19, 2021, 3:27pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/7 "2021-10-19T15:27:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 4:27pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/8 "2021-10-19T16:27:56Z")

</div>

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

addPlayer at player.js at line 92

world.js [world.js - Pastebin.com](https://pastebin.com/azeM9sdL)  
player.js [player.js - Pastebin.com](https://pastebin.com/b1Zj1Em6)

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 4:34pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/9 "2021-10-19T16:34:58Z")

</div>

> 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](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/d/d638c811f94d95323ca8d7705f257f27794a90be.png)

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

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 19, 2021, 4:37pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/10 "2021-10-19T16:37:36Z")

</div>

this.player console

 ![Screenshot 2021-10-20 at 12.35.38 AM](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/a/aee6c3f2541d32fe80b8f4a80866dea5eb78205d.png)

this.player.currentPlayer

 ![Screenshot 2021-10-20 at 12.36.07 AM](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/1/1da5227df78307bdb5e923bda4e504d2d2321a5f.png)

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [October 19, 2021, 6:33pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/11 "2021-10-19T18:33:49Z")

</div>

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

What is `playersObj` for?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [October 19, 2021, 7:05pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/12 "2021-10-19T19:05:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 20, 2021, 1:20pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/13 "2021-10-20T13:20:43Z")

</div>

> What is `playersObj` for?

They are used to store all the other players return from [socket.io](http://socket.io)  
id is the socketio.id

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

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

```

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 20, 2021, 1:27pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/14 "2021-10-20T13:27:29Z")

</div>

> 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()`

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [October 20, 2021, 2:58pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/15 "2021-10-20T14:58:09Z")

</div>

> [@stanleyseow](#):
>
> They are used to store all the other players return from [socket.io](http://socket.io)

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

> [@stanleyseow](#):
>
> When I put this.currentPlayer = this.player.currentPlayer , I get _undefined_ as during this.player.create(), the addPlayer() was not completed yet

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

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

```

Another approach is

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

```

---

<div class="post-metadata">

**Author:** ![stanleyseow](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/stanleyseow/32/1190_2.png) [@stanleyseow](https://phaser.discourse.group/u/stanleyseow)\
**Post date:** [October 27, 2021, 2:54pm UTC](https://phaser.discourse.group/t/accessing-player-physics-from-another-children-class/10474/16 "2021-10-27T14:54:15Z")

</div>

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

Thanks
