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
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);
}
}
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…
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)”.
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.