Jump to content

Access Game Object from Prototype Pattern


charlieRobinson
 Share

Recommended Posts

Hello. I am stumped.. >...<   I am using socket to detect when a new client connects and trying to call a 'createOtherPlayer' function to instantiate the otherPlayer... but I am unable to access 'game' from anywhere except for the create,update, and render functions in the prototype. Is this a phaser rule? Why am I unable to use game.add.sprite from anywhere else? 

I have seen an example where the game object is sent to an outside 

 

sharks.Game = function(game) {

}
sharks.Game.prototype = {
    create: function() {
     this.game.add.sprite  //OK
      console.log(this.game) //OK!
    },

    update: function() {
     this.game.add.sprite  //OK
      console.log(this.game) //OK!
    },
    
  
    render: function(){
        this.game.debug.spriteInfo(player, 32, 32); //OK
        console.log(this.game) //OK!
        
    },
    

    
    createOtherPlayer: function(otherPlayerName,otherPlayerID){
           console.log(otherPlayerName,otherPlayerID) //OK!
      var	otherPlayer = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'NPC');
      //Cannot access game here. Why??? I get an error.
    console.log(this.game) //UNDEFINED  why???

    }

};


sharks.otherPlayer = {
    spawnOtherPlayer: function(otherPlayerName,otherPlayerID){
    //Do I need to create other players in a function like this outside of the prototype? 
//If so, how do I send the game object from the socket function below? 
//The socket.on receives the notice that a new client has joined and calls the createOtherPlayer function.

};

socket.on('addUser', function(onlineUsers,newPlayer,newPlayerID){
					//if not this client, add new player
					if(newPlayerID!=myID){
                    //When server detects new connection, create an other play. 
//But how do you send the game object from here to be referenced?
					       sharks.Game.prototype.createOtherPlayer(newPlayer,newPlayerID);
                      }
                          //This call works but I am unable to add the sprite because I cannot reference game.***
					
}

 

Link to comment
Share on other sites

You can't call methods on the prototype directly. It's not the same as the object you created.

Is there a point in your game where you say "var game = new sharks.Game(phaserGame);" or something? You need to call createOtherPlayer on that object, like "game.createOtherPlayer(newPlayer, newPlayerId);".

Link to comment
Share on other sites

16 minutes ago, drhayes said:

You can't call methods on the prototype directly. It's not the same as the object you created.

Is there a point in your game where you say "var game = new sharks.Game(phaserGame);" or something? You need to call createOtherPlayer on that object, like "game.createOtherPlayer(newPlayer, newPlayerId);".

Hello. Thank you for your reply.

Yes. I am making the game object in the index.html section. All of the action takes place in the Game.js file. If I use game.createOtherPlayer( X, Y) then doesnt that need to be called from the index.html file?

 

(function(){
		     		
		     		var game = new Phaser.Game(screenWidth, screenHeight, Phaser.CANVAS, 'gameArea', null, true);
					game.state.add('Boot', Sharks.Boot);
					game.state.add('Preloader', Sharks.Preloader);
					game.state.add('MainMenu', Sharks.MainMenu);
				//	game.state.add('howTo', ppGame.howTo);
					game.state.add('Game', Sharks.Game);
					//game.state.add('win', ppGame.win)
					game.state.start('Boot');
					
		     	})();

 

Link to comment
Share on other sites

Ah, okay. I got a little confused.

You've got a lot of stuff going on here. shark.Game is a state. I don't think "createOtherPlayer" needs to be a method on that state, since all it does is create the other player sprite. That can happen anywhere that has access to the game.

Why not assign the socket listener inside the create method of your state? If nothing can listen until then, at least? Your "create" method would look more like this:

create: function() {
  socket.on('addUser', this.createOtherPlayer.bind(this));
},

createOtherPlayer: function(onlineUsers, newPlayer, newPlayerID) {
  // blah blah blah...
}

The problem you're seeing is that you're losing the "this" inside the callback. That's because you need to bind it while inside the callback, like I've done in the code snippet above.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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