negue

States with Parameters?

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". 

Share this post


Link to post
Share on other sites

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) {   }};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.