Search the Community
Showing results for tags 'game save'.
-
Hi, I've been working on my game project clicker (helps me learn JS and other languages). So it is going smooth so far. Thought it might be a good idea implement a save/load feature now. The following has no errors, but loading it has no effect. INITIAL VARS var player = { name:"", level:0, gold: 190,// CHANGE BACK TO 0 AFTER TESTING artifact: 0, weaponId:0, weaponLevel:0, spellId:0, spellLevel:0, goldPerClick:5, goldPerSec:0, currentTrainer:0, }; SAVE STATE /* SETUP A SAVE STATE */ function save() { // TRY TO SAVE THE GAME try { localStorage.setItem('copperSave',JSON.stringify(player)); }catch(err) { console.log('Cannot access localStorage - browser may be old or storage may be corrupt') } console.log('Game saved successfully'); }// FUNCTION: SAVE GAME LOAD STATE /* LOAD PREVIOUS game */ function loadGame() { var gameLoad = JSON.parse(localStorage.getItem("copperSave")); player= gameLoad.player; console.log(player); // this shows the starting vars NOT the saved var from getItem! } SOME MISC VARS TO HELP DEBUG initGame(); var answer = confirm('Continue from previous saves?\nWarning, "Cancel" creates a new game wiping previous saves'); if ( answer ) { loadGame(); } console.log('here after load query'); // GAME LOOP FOLLOWS BELOW.... It looks right, can anyone suggest why it might not be loading correctly. It just gives the starting variable, not the saved variables.
- 5 replies
-
- javascript
- game save
-
(and 1 more)
Tagged with: