Jump to content

Carry score over through resets and new levels?


joshcawthorne
 Share

Recommended Posts

Hi all,

I have code for a crystal count within my level at the moment, however when the player dies and the level resets, or when the player moves on to level 2 (in a new js file), the score resets as I've initiated it within level 1.

Could someone please tell me how to create a variable that can be tracked and edited in every JS file, rather than just one?

Thanks!

Link to comment
Share on other sites

Globalising variables is the only way to share state between different script includes, `var score` gets translated to `window.score` in the browser and window is available to all of your scripts but JS has shorthands for accessing window properties (its probably best to refer to these as root scope, but in the browser this is the window object).

The alternative is to wrap your code up using a module system, such as browserify or webpack (for example) but this is a far more involved way to solve your issue of sharing state across files and you might want to crack on and finish up this project first before looking to see how a module system can help you to write better code. On a side note Safari have just shipped a module implementation to a dev channel so real strides are being made to sort this out in the browser!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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