Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

StateManager and global variables working across files?


bearkin333
 Share

Recommended Posts

Hello there again. I'm making good headway on my project, but as it grows, I find myself needing to not only split the code into separate files, but start using a State Manager. I set out to do these things, but I've run into an obstacle I did not expect. I'm trying to make a different .js file for each state I use, and I include those in the main .html file that houses the game via the <script> tag at the top. However, due to this, my state files are having trouble figuring out where the ThisGame object is. Here's my main code, split into the pieces that should matter.

  var game = new Phaser.Game(DEFAULT_GAME_WIDTH, DEFAULT_GAME_HEIGHT, Phaser.AUTO, ''); ThisGame = {};    window.onload = function() {        game.state.add('levelSTATE',ThisGame.levelSTATE);    game.state.add('overview_menuSTATE',ThisGame.overview_menuSTATE);        game.state.start('levelSTATE');};        

And the file for the levelstate, with the functions stripped for clarity

ThisGame.levelSTATE = function(){ };ThisGame.levelSTATE.prototype = {        preload: function() {    },        create: function() {    },        update: function() {    }}

By all accounts, this should be working perfectly, but levelSTATE says that ThisGame was never defined. I guess there's something about the nature of everything in this scenario that I don't understand, but can anyone give me some pointers on what I'm doing wrong?

Link to comment
Share on other sites

It's really frustrating when problems that I've been wrestling with for hours have an easy, ridiculous fix.
All the examples I'd seen with state managers used a "BasicGame" thing to add the states too - but they were also really old. Thanks. Just removing that fixed everything.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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