We will need to store persistent data locally on the device. This can be level details to start the game where the user left off the last time they played, or can be preferences data such as the sound volume, sound on/off, or even the current local leaderboard. Saving the current game state occasionally to pick up where we left off is a recommended feature for any mobile game. Local data can be stored in multiple ways by using SharedObject
, EncryptedLocalStore
, a custom file, or even an sqlLite
database.
Data access using a SharedObject
method can be done in the following manner:
private static var so:SharedObject=SharedObject.getLocal("some unique name"); public function store(gameData:Object):void{ so.data[gameName]=gameData; so.flush(); } public function retrieve():Object{ return(so.data[gameName]); }
With EncryptedLocalStore
it is a bit different, as shown in the following code:
private function saveItem(key:String, value:String):void { var bytes:ByteArray = new ByteArray(); bytes.writeUTFBytes(value); EncryptedLocalStore.setItem(gameName+key, bytes); } private static function loadItem(key:String):String { var storedValue:ByteArray = EncryptedLocalStore.getItem(gameName+key); return(storedValue.readUTFBytes(storedValue.length)); }
You may use FscoreBoard by Sean McCracken (@Seantron) and Jesse Freeman (@CodeBum) for creating a local leaderboard (https://github.com/gamecook/FScoreboard).
Using the SqlLite
database is a bit more complicated and is to be considered when there is a lot of data to be stored. You can check the Adobe documentation with the link http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118676a5497-7fb4.html.
3.148.103.210