espace Posted April 7, 2017 Share Posted April 7, 2017 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 More sharing options...
rblopes Posted April 8, 2017 Share Posted April 8, 2017 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 More sharing options...
espace Posted April 8, 2017 Author Share Posted April 8, 2017 Thanks https://jsfiddle.net/qczvwz8e/ Solved Link to comment Share on other sites More sharing options...
espace Posted April 8, 2017 Author Share Posted April 8, 2017 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 More sharing options...
Stvsynrj Posted April 8, 2017 Share Posted April 8, 2017 Hi ! You should do it that way : var projectile = JSON.parse( localStorage.getItem('_projectile')); if (projectile) this.projectile = game.add.sprite(projectile.x,projectile.y,'image') else this.projectile = game.add.sprite(100,300,'image') espace 1 Link to comment Share on other sites More sharing options...
espace Posted April 8, 2017 Author Share Posted April 8, 2017 final solution hope it help someone and thanks again to the contributor https://jsfiddle.net/dy0hok9h/2/ rblopes 1 Link to comment Share on other sites More sharing options...
spinnerbox Posted April 8, 2017 Share Posted April 8, 2017 The circular error you get is because perhaps, somewhere in the object there is a reference which points to the same object which contains the reference aka "this". Link to comment Share on other sites More sharing options...
espace Posted April 13, 2017 Author Share Posted April 13, 2017 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 More sharing options...
espace Posted April 13, 2017 Author Share Posted April 13, 2017 ok resolved > i can easy see the content of my localstorage. simply run this command in the console in devtool mode JSON.stringify(localStorage) then i copy paste the result into a local file and i use this example to load in my game : http://phaser.io/examples/v2/loader/load-json-file Link to comment Share on other sites More sharing options...
Recommended Posts