Jump to content

How to call stage method outside stage


ZeroGravity
 Share

Recommended Posts

I am new to Phaser. I am trying to create a simple online game, which would involve asynchronous calls to game stage outside of the game (using sockets). But I am not able to get simplest async thing working. Here is my problem.

 

I have following code (this is one of the my game stages), where I am:

 

1) calling socket creation

2) creating socket and waiting for server to respond

3) when server responds anything

4) I show button (text, sprite, ... anything) .. and FAIL...

var selectroom = function(game) {};selectroom.prototype = {    preload: function() {    },    create: function() {      socketConnect();   // (1)    },    showBtn : function() {      // (4)      this.game.button = this.game.add.button(10,10, 'btn', selectRoomBtn, this, 0,0,1,0);    }  };function selectRoomBtn(that) {  that.game.state.start("playfield");}function socketConnect() {    globalWebsocket = new WebSocket('ws://localhost:6789/');      // (2)    globalWebsocket.onopen = function() {      console.log('open');      sendToServer('login','hello');      selectroom.showBtn();    };      // (3)    globalWebsocket.onmessage = function (evt) {       selectroom.showBtn();    };}

The error I get is:

Uncaught TypeError: Object function (game) { } has no method 'showBtn' selectroom.js	

But if I check with debugger object selectroom has method showBtn.

 

 

The code above fails even with timer (removing all that socket hassle):

function socketConnect() {  setTimeout(function() {    selectroom.showBtn();  }, 2000);}

Even this does not work, same error. What in earth am I doing wrong???

 

I tried to find out some event wakeup methodology, but could not find any. I dont understand how should I sort this out (I even tried to save this game object as _game object like this, even this didn't work. Like this:

var selectroom = function(game) {  this._game = game;};...this._game.button = this._game.add.button(400,136, 'btn', selectRoomBtn, this, 0,0,1,0);...

Same error. Apparently I dont understand something about the scope...

Link to comment
Share on other sites

Apparently I have understood things wrong and I still might.

 

This state 'selectroom' is not a such object that would hold the game instance. It's just what its looks like, a java object with few methods. Then calling 'game.state.add('selectroom',selectroom);' will add it to games stages, and game will use these stages as kind of code blocks (when ever execution is in that stage) calling right interfaces (create, update, preload...).

 

So, that lead to me, how do I get my button there? I made a mistake that was not seen in code shown above. I did not define 'game' object as global, so I could not access it from anywhere else than from the stages. So I made 'game' object as global and used that, simple as that (stupid me). So remove that showBtn from stage prototype, and simple add the button to the stage:

globalWebsocket.onmessage = function (evt) {   game.button = game.add.button(10,10, 'btn', selectRoomBtn, game, 0,0,1,0);}... also I changed this now ...function selectRoomBtn() {  game.state.start("playfield");}

Here is a simple jsFiddle of it: http://jsfiddle.net/J5fUE/168/

 

So its working for me now, but if you have insights please share.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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