Jump to content

re-use state for different maps


P4UL
 Share

Recommended Posts

Hello!

I have a game with multiple states. The logic in all states is the same. The only thing that changes is the tilemap used in the create function. All other objects are the same in all states.

I could simple duplicate one file with the state for every tilemap. Is there an easier way to do this? I would really like to re-use one state for all tilemaps.

As far as I understand how states work the create function will be called every time the state is started. So I believe there must be a way to give the name of the tilemap as a parameter to the create function or something similar.

Thanks in advance!

Link to comment
Share on other sites

I had not thought about class inheritance. In the meantime I tried to store the name of the next tilemap in a global variable and reading this variable in the create function. It worked but your idea is much better and cleaner. I will rewrite my code to use class inheritance for the states, thank you very much!

Link to comment
Share on other sites

Hi, when you call game.state.start, you can add multiple optional parameters:

game.state.start("Mystate", true, false, param1, param2);

These parameters are then passed to init() method, if you have any in your state (init is called as first before create()).

With this, you can pass variables from state to state. You can still combine it with inheritance solution and have cleaner code.

Link to comment
Share on other sites

You can also use 

game.state.states['MyState'].myGlobalVariable = "Value";

And this can be called from all other states. This is what i use to carry a variable between states.

I find this to be much easier then the other examples, however the downside is that you get really big variable names.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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