Ninjadoodle Posted October 17, 2014 Share Posted October 17, 2014 Hi guys I understand how to setup Local Storage and how to store values in it. I'm trying to save level progress, but need to check whether the Local Storage is empty on the first play, in order to send the player to the first level. 1. Is this the correct way to do this and will this work across multiple browsers?2. This doesn't seem to work with CocoonJS3. Also, could somebody please explain to me, what the zero after 'level' stands for? if (game.storage.get('level', 0) == null) { game.system.setScene('Level1');} else { game.system.setScene(game.storage.get('level', 0));} Thank you heaps in advance! Quote Link to comment Share on other sites More sharing options...
chg Posted October 17, 2014 Share Posted October 17, 2014 In the Panda.js API the 2nd parameter is the default value as per http://www.pandajs.net/docs/classes/game.Storage.html#method_get The proper way to check for the existence of a key would seem to be to use the has() method - asking the get method to return the number 0 if a key is not found and comparing this to null with == seems a little wacky to me :| (Actually, calling get() again after the above test with the same parameters seems wacky to me too, as does then blindly using the value without further range checking). Your code also doesn't do what you describe - rather than checking that local storage is empty - it merely checks for a specific key. If you want to check if it is empty it looks like you might have to bypass Panda's API and check localStorage.length === 0, but that seems a bit ugly to me especially if you feel you might want to extend the system yourself later, for example to store the data using some obsfucation... Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 17, 2014 Share Posted October 17, 2014 I'm not sure if this is the right method or not, but I use the parseInt function that parses a string and returns an integer:currentLevel = parseInt(game.storage.get('currentLevel')) || 0;if currentLevel is not an integer (cause doesn't exists), it will be set to "0". Than select the level based on currentLevel value. I used this way also for initial settings parameters and as trick to show the tutorial at first game start. Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted October 17, 2014 Author Share Posted October 17, 2014 Thank you chg and Phempt I've only just looked into local storage and I'm sure I'm getting it completely wrong. Phempt - Would this be the correct way of using your technique? currentLevel = parseInt(game.storage.get('currentLevel')) || 0;if (currentLevel == 0) { game.system.setScene('level1');} else { game.system.setScene(currentLevel);}One thing I'm still wondering tho, is whether it's possible to get this working with Cocoon JS or what the alternative would be. Thank you again for the help! Quote Link to comment Share on other sites More sharing options...
Phempt Posted October 17, 2014 Share Posted October 17, 2014 game.system.setScene("level"+currentLevel);so you can set as Scene "Level1", "Level2"....."levelN". I never used a number in scene name, but I think it works. Quote Link to comment Share on other sites More sharing options...
enpu Posted October 17, 2014 Share Posted October 17, 2014 This should work:var currentLevel = game.storage.get('level', 0);Second parameter is default value, so if there is no 'level' found in local storage, it will return 0. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted October 17, 2014 Author Share Posted October 17, 2014 Awesome, thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.