Jump to content

Where to put global var in the basic template


Biggerplay
 Share

Recommended Posts

Where can I put a global game var in the basic template and then be able to access it from any of the game states?

 

So for example I want to have gold var, which is the players gold amount, and I need that to increase each time a new level runs, but I can't quite figure out where to put it, and then access it. I've tried create a this.gold = 0, and a var gold = 0, in boot.js, in MainMenu.js etc, but I can't access it from HUD.js which is where I need to access it from.

 

 

Link to comment
Share on other sites

If you need to save variables between multiple states, you can store them as your main global var's properties. I think the main global variable is called BasicGame in the basic template.

 

For example:

BasicGame.gold = 0;

Saving the value to this.gold inside a state wouldn't work, as all the state values are flushed on state change.

Link to comment
Share on other sites

Look at the 'Full Screen Mobile' template Boot.js, the top part:

BasicGame = {    /* Here we've just got some global level vars that persist regardless of State swaps */    score: 0,    /* If the music in your game needs to play through-out a few State swaps, then you could reference it here */    music: null,    /* Your game can check BasicGame.orientated in internal loops to know if it should pause or not */    orientated: false};
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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