Sign in to follow this  
unklebooey

Local Storage/JSON parsing error with Arrays in Phaser 2

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!

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.