Sunday, October 20, 2013

How to save data/records of the Android Games in Adobe AIR AS3?

Throughout the game play (either Android Game or others), the player's record is important to be always updated in order to let him/her continue the previous checkpoint. From the complicated RPG Games (levels, equipments, cash left, etc) to the simple mini games (high score, number of attempts, number of stages unlocked), records are necessary so that the players need not start all over again every time they engage the game.

In fact, the feature of saving data/records of the Android Games in Java is available here. The question is, how to implement the same thing in Actionscript 3?

1. import flash.net.SharedObject
The code which is similar to the SharedPreferences in Java is the "SharedObject". To use it, you need to import the flash library.

import flash.net.SharedObject

2. Define a SharedObject variable and a Record name
Define a variable named "shared" at which the records would be stored. You can provide any "RECORD_NAME" to create a shared object.

private var shared:SharedObject = SharedObject.getLocal("YOUR_RECORD_NAME");

 3. Define your data in Object datatype and store it inside the 'data'

The "data" member of the SharedObject is nothing different from the datatype "Object" - an associative array. You can easily define your records in "Object" datatype before storing it.

For example, to store the levels, hit points, mana point of a character, the codes would be as below:

shared.data.characterData = {"level":5, "hit_point":100, "mana_point":100"};
 
In short, it isn't hard to have a data saving feature in your Android Game. So, next time when designing the game architecture, remember to have this feature included.

No comments:

Post a Comment