Jump to content

Adding additional paramaters to states doesn't work - Typescript


Obliviadge
 Share

Recommended Posts

So I want to make states which can have custom constructor parameters of some sorts.

This is my testState:

export class TestingState extends Phaser.State
{
    constructor(firstVar: any, secondVar: any, thirdVar: any)
    {
        console.log(firstVar);
        console.log(secondVar);
        console.log(thirdVar);
        super();
    }
}

The firstVar is always the Phaser.Game variable, but the second and third are supposed to be custom parameters.

I start my state like this:

this.game.state.add('testState', GameStates.TestingState, false);
this.game.state.start('testState', false, false, 'Hello', 'test');

The firstVar just logs the Phaser.Game and the second and third print out undefined. But if I remove the 'test' argument, it doesn't log anything the third time.

Link to comment
Share on other sites

Hi, this is part of add method in StateManager:

        if (state instanceof Phaser.State)
        {
            newState = state;
        }
        else if (typeof state === 'object')
        {
            newState = state;
            newState.game = this.game;
        }
        else if (typeof state === 'function')
        {
            newState = new state(this.game);
        }

So, if you call:

this.game.state.add('testState', GameStates.TestingState, false);

there will always be only one parameter, beacuase it will go through instantiation in last branch of if-else

But you can first create your state and then pass ready instance to add method of StateManager:

var myState = new GameStates.TestingState(myParam1, myParam2, ...);
this.game.state.add("myState", myState);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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