localGhost Posted March 30, 2014 Share Posted March 30, 2014 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 More sharing options...
localGhost Posted April 1, 2014 Author Share Posted April 1, 2014 Third option, or rather refinement of the first one:game.state.states[game.state.current].clickedOnSprite(this.number);Somehow I feel there should be an easier way to access the current state... Link to comment Share on other sites More sharing options...
Hsaka Posted April 1, 2014 Share Posted April 1, 2014 Hey, you could simply pass the instance of the state as a parameter to your custom class.//in Playstate:var custom = new CustomClass(this); Link to comment Share on other sites More sharing options...
localGhost Posted April 1, 2014 Author Share Posted April 1, 2014 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 More sharing options...
Recommended Posts