GoodOldSnoopy Posted March 30, 2017 Share Posted March 30, 2017 I'm doing a round based game. So after you've killed so many enemies it'll pop up, "Round 1", after some more, "Round 2" and so on. The amount of enemies doesn't change it's just the round that increases after you've killed say 20 enemies. I have a current variable at the top of my game.js file var currRound = 1; var EnemiesKilled = 0; Every time I kill an enemy I increment up the variable then in one of the collision handlers I do if EnemiesKilled == 20 then show some text newLevel.setText("Woo!! Round " + currRound + "!"); and restart the round via setTimeout(function() { game.state.start(game.state.current); }, 1000); The thing is, it whipes the variable of EnemiesKilled. How can I keep that variable so I always know how many enemies the player has killed? Link to comment Share on other sites More sharing options...
mattstyles Posted March 30, 2017 Share Posted March 30, 2017 Where does it wipe it? Is there something in your state start function that resets it to 0? If so then either nuke that line or create a new variable to record enemies killed per round, if you need that, or total enemies. Link to comment Share on other sites More sharing options...
GoodOldSnoopy Posted March 30, 2017 Author Share Posted March 30, 2017 I think it might of been something I was doing wrong. I found another question similar on forums and ended up going for this approach this.game.state.states['play']._currentCount = EnemiesKilled; game.state.start('play'); This seems to work fine Link to comment Share on other sites More sharing options...
mattstyles Posted March 30, 2017 Share Posted March 30, 2017 Thats a really bad fix, totally breaks encapsulation, but, if you're cool with that and it works then crack on! Link to comment Share on other sites More sharing options...
MikeW Posted March 30, 2017 Share Posted March 30, 2017 I would move the variables up a level in scope. Global variables are generally a bad idea in programming, however in games certain things do need to be remembered on a global level Link to comment Share on other sites More sharing options...
mattstyles Posted March 30, 2017 Share Posted March 30, 2017 4 minutes ago, MikeW said: however in games certain things do need to be remembered on a global level I disagree, some things are sometimes easier when global, but you risk unleashing zͪaͥͤ̈́̅͊̌ͥl̓̃̇̌gͬ̄o͋̑̇̓ͬ. Link to comment Share on other sites More sharing options...
MikeW Posted March 30, 2017 Share Posted March 30, 2017 7 minutes ago, mattstyles said: I disagree, some things are sometimes easier when global, but you risk unleashing zͪaͥͤ̈́̅͊̌ͥl̓̃̇̌gͬ̄o͋̑̇̓ͬ. That is why I said Global variables are generally a bad idea in programming, Link to comment Share on other sites More sharing options...
MikeW Posted March 30, 2017 Share Posted March 30, 2017 In C++ I used a pointer to a an object I called Universe and I passed a pointer to the universe to every function I wrote except for some basic utility - not sure how to duplicate that in javascript Link to comment Share on other sites More sharing options...
mattstyles Posted March 30, 2017 Share Posted March 30, 2017 So nothing needs to be global! We agree! awesome Link to comment Share on other sites More sharing options...
Henrique Posted March 30, 2017 Share Posted March 30, 2017 I would do one of the following: State Change Params Temp Save Game State (localstorage, indexeddb/websql...) Maybe create your own class to persist/manage these variables/states.. (extends Phaser.State) Link to comment Share on other sites More sharing options...
mattstyles Posted March 30, 2017 Share Posted March 30, 2017 4 hours ago, MikeW said: In C++ I used a pointer to a an object I called Universe and I passed a pointer to the universe to every function I wrote except for some basic utility - not sure how to duplicate that in javascript You could do something similar using a module system (commonJS etc). Link to comment Share on other sites More sharing options...
Recommended Posts