Jump to content

Cannot read property 'cache' of null || this.game is null (Phaser.js 10)


christian.tucker
 Share

Recommended Posts

The game that I'm working on is multiplayer and makes use of nodejs and socket.io -- Basically when a player disconnects from the server they are removed from the client, this is done through the below code:

socket.on('player-disconnected', function(json) {    networkPlayers[json.playerId].finalize();    delete networkPlayers[json.playerId];});

Where:

var networkPlayers = {};

Inside the finalize() method of the player, the following code is executed:

Player.prototype.finalize = function() {    this.finalized = true;    this.playerSprite.destroy();    this.text_floating_name.destroy();};

This works flawlessly, except for one condition, When a player logs out / disconnects they're no longer handled and they're removed from all clients; However if the client has not been updated after the removal and another player is added I receive the following error:

 

Opera:

ncaught TypeError: Cannot read property 'cache' of null

FireFox:

TypeError: this.game is null phaser.min.js:10

More information points to this method

Player.prototype.stopWalking = function() {    if(this.finalized == true) {        this.playerSprite.animations.stop();        this.playerSprite.loadTexture('warrior');    }};

which is called when a tween event finished; The error is reproduced by disconnecting the client (and therefor calling destroy()) on the sprite before the tween event is called.

 

I wrapped it in a finalized check to try to stop the error from occuring but it didn't work, the error is specifically pointing to this line:

this.playerSprite.loadTexture('warrior');

I'm not exactly sure how to go about fixing this anymore, unless I should make the movementTween a prototype variable instead of a local function variable and do something with it that way. 

 

 

Note: The game doesn't freeze, only the rendering, the logic and input still continues to be processed and relayed over the network, but the canvas stops updating at this point. The reason I call loadTexture at the end is to set the sprite back to it's normal state after the animation is stopped. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...