Jump to content

Restart state but keep saved variable?


GoodOldSnoopy
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

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