Jump to content

Local Storage/JSON parsing error with Arrays in Phaser 2


unklebooey
 Share

Recommended Posts

 

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

 Share

  • Recently Browsing   0 members

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