ImpactStorage is a plugin for HTML5/js game framework ImpactJS, giving developers an easy-to-use interface to localStorage for their projects. Mostly a facade for localStorage, ImpactStorage is set up to bake with your ImpactJS project, take care of some error handling, some convenience methods, as well as support objects in addition to strings.
Created by Jordan Santell for Duck It! (oh god, I need to finish this); enjoy!
- Cleaned up a lot of redundant syntax
- Methods return
null
if the browser doesn't support localStorage --isCapable()
should still be used - Added a minified version
- Move impact-storage.js to your ImpactJS project's plugin folder:
[PROJECT]/lib/plugins/impact-storage.js
- Add the plugin to your main.js file
ig.module( 'game.main' ) .requires( 'impact-game', ... 'plugins.impact-storage', ... )
- Create a local storage object and check out the examples and methods below
storage: new ig.Storage(),
isCapable()
: Returnstrue
if the browser is capable of using localStorage.false
otherwise.isSet(key)
: Returnstrue
ifkey
has been set in localStorage.false
otherwise.initUnset(key, value)
: Iffkey
has not been set, setkey
tovalue
get(key)
: Returns the value associated withkey
in localStorage as astring
, or anobject
if parsable by JSON.getInt(key)
: Returns the value associated withkey
in localStorage as anint
.getFloat(key)
: Returns the value associated withkey
in localStorage as afloat
.getBool(key)
: Returns the value associated withkey
in localStorage as abool
. Returnsnull
ifvalue
is not0
,1
,false
ortrue
.key(n)
: Returns the value of the key stored at positionn
in localStorage.set(key, value)
: Sets an item in localStorage with thekey
value
pair. Overwrites the previous value ofkey
if it existed previously.value
is stored as either astring
or anobject
.setHighest(key, value)
: Sets an item in localStorage with thekey
value
pair iff the currently storedvalue
is smaller, then returns true. Otherwise returns false.remove(key)
: Removes the item with the specifiedkey
.clear()
: Clears all localStorage data associated with this origin.
this.storage = new ig.Storage(); // Initialize high score as 0 if 'highScore' does not exist this.storage.initUnset('highScore', 0);
During the update loop that determines whether or not the current score should override the score in localStorage:
var player = this.getEntitiesByType(EntityPlayer)[0]; /* Updates the value of 'highScore' if and only if player.score > this.storage.get('highScore') */ this.storage.setHighest('highScore',player.score);
localStorage stores all data as strings, but the ImpactStorage .set(key, value)
and .get(key)
methods convert objects to and from strings in order to be saved to localStorage.
this.storage = new ig.Storage(); /* Player's velocity is an object stored as vel: {x: 200, y: 100} And that data is now being stored with key playerVel in localStorage */ var player = this.getEntitiesByType(EntityPlayer)[0]; this.storage.set('playerVel',player.vel) // And let's output it for fun alert("Player's x velocity: "+this.storage.get('playerVel').x); alert("Player's y velocity: "+this.storage.get('playerVel').y);