Jump to content

States with Parameters?


negue
 Share

Recommended Posts

Hey, 

 

i'm wondering if this feature maybe needed in Phaser.

 

We all can use States, which is very nice and easy to separate files / logic, but we can't "easy" pass parameters from the MainMenu to the "Game"-States.

 

With easy I mean something like this:

game.state.startWithParams('name', { myParam1: true });// inside the State on the constructorfunction myState(game, params){// {...}}

Ofcouse I could just attach my parameters to my gameObject.  :D

 

How do you handle your parameters to your "main game state". 

Link to comment
Share on other sites

  • 4 months later...

You can pass additional parameters via game.state.start();

e.g.

game.state.start('Leaderboard', true, false, customParam1, customParam2);

The first two params tell Phaser whether or not to clear the game world and cache.  Any params after those are custom and will be sent to your state's 'init' function if it exists.

 

Then within your state you need to create the init function:

 

  var LeaderboardState = function() {  };  LeaderboardState.prototype = {    init: function(customParam1, customParam2) {   }};
Link to comment
Share on other sites

  • 4 months later...
I read here that you can reference variables through the StateManager, something like this.game.state.states['statename'].variable, see code below



// Phaser.state for level select screen
mygame.LevelSelect = function(game){
    this.game = game; // keep reference to main game object
    this._selectedLevel = 0;
};
mygame.LevelSelect.prototype = {
    create: function(){
    //etc.
    onLevelSelected: function() {
        // pass variables to 'MainGame' state
        this.game.state.states['MainGame']._currentLevel = this._selectedLevel;
        this.game.state.states['MainGame']._showTutorial = (this._selectedLevel == 1); // only show tutorial on level 1
    },
    //etc.
};

// Phaser.state for main game loop
mygame.MainGame = function(game){
    this.game = game; // keep reference to main game object
    this._showTutorial = false;
    this._currentLevel = 0;
};
mygame.MainGame.prototype = {
    create: function(){
    //etc.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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