Jump to content

local storage with JSON error


espace
 Share

Recommended Posts

hi,

i would use the local storage to store value of my object. i set the object draggable and when the drag stop i would reveal parameter of this object.

the problem is that i have this error : Uncaught TypeError: Converting circular structure to JSON

i follow this part of tutorial :

var car = {};
car.wheels = 4;
car.doors = 2;
car.sound = 'vroom';
car.name = 'Lightning McQueen';
console.log( car );
localStorage.setItem( 'car', JSON.stringify(car) );
console.log( JSON.parse( localStorage.getItem( 'car' ) ) );

But in my case that don't work. Why ?

Thanks for your help.

https://jsfiddle.net/espace3d/cmd0158g/

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create });

function preload() {
game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png');
}

function create() {
this.projectile=game.add.sprite(200,200,'circle')
this.projectile.number=1
this.projectile.name="weapon"
this.projectile.inputEnabled=true
this.projectile.input.enableDrag(true)
this.projectile.events.onDragStop.add(reveal_param,this)
}

var reveal_param=function(sprite){
localStorage.setItem('_projectile', JSON.stringify(sprite));
var _projectile_var= JSON.parse( localStorage.getItem( '_projectile' ) ) ;
alert(_projectile_var.name)
}

 

Link to comment
Share on other sites

That way you're trying to save a whole game object to localStorage.

Rather than storing the whole sprite object just to retrieve a few properties later, what you really want is to pick just the properties you're interested in restoring later.

Example:

// To store in `localStorage`:
function reveal_param(sprite){
  var projectile = {
    name: sprite.name,
    number: sprite.number,
    // ... etc ...
  };

  localStorage.setItem('_projectile', JSON.stringify(projectile));
}

// To recover that object later:
var projectile = JSON.parse( localStorage.getItem( '_projectile' ) ) ;
alert(projectile.name);

 

Link to comment
Share on other sites

Just one question. How do you test if there is a local storage and then load them in the place of the first parameter.

i imagine do that, but it's not correct

if(projectile){

// To recover that object later:
projectile = JSON.parse( localStorage.getItem( '_projectile' ) ) ;

this.projectile=game.add.sprite(projectile.x,projectile.y,'image')

}else{

this.projectile=game.add.sprite(100,300,'image')

}

 

Link to comment
Share on other sites

hi,

For circular error i have found this library to avoid this problem : https://github.com/WebReflection/circular-json

Yet another question.

Now i can design each of my level and store them in the localstorage.

But how do you do when the settings are ok to upload the localstorage in a real json file (store really the file in the structure of the game)  ?  if i go to the path of the localstorage where the json file is store, the file doesn't look like a json file ...

example :

59viewMmapStatusMmapStatusCREATE VIEW MmapStatus (value) AS SELECT -1z!11tableItemTableItemTableCREATE TABLE ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)[!}1indexsqlite_autoindex_id3E_canon_local00{"frequency":90,"x":1160,"y":1240}0Ipmygame_progress[3,2,3,0,-1,-1,-1,-1,-1,-1,-1,-1,

what's the method to get the value of this file directly in a real json file ?

thanks ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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