In the task, we store the game progress locally. When a player loads the game, we use the world time to calculate the building construction.
We will use the following steps to store game parameters and load them at the initial stage:
Create the
saving
function with the following code:game.autoSave = function() { if (cjs.Ticker.getTicks() % 100 === 0) { localStorage['city.coins'] = game.coins; localStorage['city.diamonds'] = game.diamonds; localStorage['city.buildinglist'] = JSON.stringify(game.buildingsList); } };
Add the
saving
function to the ticker so that it can autosave:cjs.Ticker.addEventListener('tick', game.autoSave);
Now, we can load the saved game using the following code:
if (localStorage['city.buildinglist']) { game.buildingsList = JSON.parse(localStorage['city.buildinglist']); } else { game.buildingsList = []; }
When we initialize coins and diamonds, we load the values from the local storage:
game.coins = localStorage...