States with Parameters?

Recommended Posts



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();


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['statename'].variable, see code below

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

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

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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.