Jump to content

Calling the state's method from custom class


localGhost
 Share

Recommended Posts

I have the following setup:

1. I use states, i. e. I have, like, game.state.add('Playstate', Main.Playstate); and later game.state.start('Playstate');

2. I have a custom 'class', i.e. object not tied to Phaser, but using inside something from it. In particular, in this class I have a sprite which should accept clicks:

this.image = game.add.sprite(myTopX, myTopY, rect);this.image.inputEnabled = true;this.image.events.onInputDown.add(this.onClickHandler, this);

3. From this class' onClickHandler I want to call a function from PlayState and pass some info, for example, which exactly object of this class has its sprite clicked.

Maybe I'm overcomplicating, and pointing that out also might be of help. But anyway so far I tried and see either of the following lines working as I want:

game.state.states.Playstate.clickedOnSprite(this.number);game.state.callbackContext.clickedOnSprite(this.number);

Then, in the clickedOnSprite function I may take appropriate action.

Which one would be preferred/working without problem? What if I'll have several states, and my objects could be created from either of them?

 

I also see a third way, passing the desired function as a parameter to the 'class', for example, when creating an instance. Maybe that's the best way?

Thanks.

 

Link to comment
Share on other sites

I can but I don't think it's 'simply' :)

My custom class already has reference to game (or rather access to global variable game), so why pass another argument?

Generally (not in my specific case though), there may be enough arguments already to be passed...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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