Error on enableing physics

Hello there !
I’m making a multiplayer game and I want to change the player scene.
When I change the scene, I can’t re-enable the physics on that scene !!

My error occurs with this line of code.
self.physics.world.enable(self.player);
(self is the current scene)
The error : Uncaught TypeError: Cannot read property ‘enable’ of null

Thanx for your upcoming support :slight_smile:

What’s the stack trace (click :arrow_forward:︎)?

Likely the scene self is shutdown at that point.

You probably need to remove that event listener when the scene stops.

I tried it but I’m not sure how to deal with it while it’s in a update loop. I tried several things like “if(self)” il the loop but still, doesent seems to be working.
Here is the part where I call this function:

update() {
if (this.player) {
if (this.player.x < 0) {
socket.emit(‘preQuit’);
this.scene.start(‘Scene2’);
}
}
if (this.player) {
player_gameplay(this);
}
}

OK, I got it.
I am using Scene.start instead of Scene.switch ! So the rigth way was :

this.scene.switch('Scene2');

Thx for helping :slight_smile:

1 Like

It’s working but I’m running into another issue now. What I did was just start the ‘Scene2’ while the other one was running behind it. That’s something I neet to fix bc I want the create event to fire every time I join in. Another event could work but I don’t really want to mess with that.
I just want to remove the scene (like this.scene.start(scene) does) but then I run into the physics issue…

Go back to scene.start(), I think.

If you’re adding event listeners in the scene create(), they need to be removed when the scene is stopped.

I’m not sure what type of listeners you are talking about. Socket listeners ? If so how sould I remove them ?

I think socket listeners, yes. Whatever is calling addOtherPlayers. How/where are you adding them?

So I initialise the socket one on login, then no more. I call the addOtherPlayers function whenever I join a scene. My issue is that when changing scenes, the listener on Scene1 doesent stop, even if the scene is “deleted” with “self.scene.start(Scene2)”.

What’s the listener on Scene1?

“socket.on(‘newPlayer’, …)”
It’s fired whenever a player logs in, or switch scenes

Can you show the code for that?

Server-side:

socket.on('changeMap', (data) => {
  socket.emit('currentPlayers', player);
});

where player[someId] is an object containing all player info.

Client-side:

create(){
  // sending to server the "current" map
  socket.emit('changeMap', idOfMap);

  // if player is yourself, then addPlayer() 
  socket.on('currentPlayers', function (players) {
    Object.keys(players).forEach(function (id) {
      if (players[id].playerId === socket.id) {
        addPlayer(self, players[id]);
      }else if(players[id].currentMap === currentMap){
        addOtherPlayers(self, players[id]);
      }
    });
  });
}

addPlayer() and addOtherPlayers() are basicly the same, just adding them to different containers.

Something like

class Scene {
  create () {
    socket.on('currentPlayers', addCurrentPlayers, self);

    this.events.once('shutdown', function () {
      socket.off('currentPlayers', addCurrentPlayers, self);
    });
  }
}

It works ! But now when I re-enter the First scene, the “addPlayer()” fires after the update()…

This topic should help if someone is running the same issue