unklebooey Posted February 10, 2020 Share Posted February 10, 2020 I'm fairly new to Phaser 2 and using Local Storage. I am getting errors when parsing my levels array. 0 is unlocked, 1 is locked. Unlocking the first 2 levels works great with Local Storage. When unlocking level 3 and refreshing the page, I get the following error: "Unexpected token , in JSON at position 1" in the boot.js file: if (JSON.parse(localStorage.getItem(game.global.localStorageName)) === null) { localStorage.setItem(game.global.localStorageName, JSON.stringify(game.global.levelsArray)); } else { game.global.levelsArray = JSON.parse(localStorage.getItem(game.global.localStorageName)); } Setting local storage after completing a level: if (game.global.levelsArray[game.global.level] == 1 && game.global.level < game.global.levelsArray.length) { game.global.levelsArray[game.global.level] = 0; localStorage.setItem(game.global.localStorageName, JSON.stringify(game.global.levelsArray)); } Initial global variables in game.js game.global = { levelsArray: [0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,], level: 0, localStorageName: 'savedLevels' } Thanks! Link to comment Share on other sites More sharing options...
Jammy Posted February 16, 2020 Share Posted February 16, 2020 console.log(localStorage.getItem(game.global.localStorageName)) and then figure out whats gone wrong with the json. Link to comment Share on other sites More sharing options...
Recommended Posts