0

I want to ask what is the best way to save some data for an educational mobile app I want to create with flash. Is it

  • shared objects or
  • XML file?

I want to save data like

  • username,
  • wrong / correct attempts,
  • time to complete
  • etc.

I don't want the app to ask the child where to save the file.
Also I want - each time the child use the app - the new data to append to the old data.
A link to a tutorial would be very useful.

zx485
  • 28,498
  • 28
  • 50
  • 59

2 Answers2

2

If you don't care if the saved info is human readable (or prefer it that way), a good way to do this is to use serialized objects in either a save file, or a shared object.

The process is pretty similar regardless of which storage mechanism used. Here is an example.

First, make a global var for your shared object (probably at the start of your application):

    var sharedObj:SharedObject = SharedObject.getLocal("MySaveData");

    //if data exists in the shared object, load it or do something with it
    if(sharedObj.data.userName){
        trace("Hello " + saveData.userName);
    }

Later, when you want to save some data:

    sharedObj.data.userName = "fflinstone";
    sharedObj.data.wrongAttempts = 5;
    //etc for other properties you want to add

    sharedObj.flush(); //write the data to file

Keep in mind, that any non-primitive class you use, must be registered to unserialize them.

So, if you wanted to save a type of object that required an import statement (like a Rectangle for instance) you'd have to register that by doing:

flash.net.registerClassAlias("flash.geom.Rectangle", Rectangle);  //you only have to have this once in your whole application, usually at the start of your program

Alternatively, you could create a model for your save data (a custom class).

To do it that way, create a new class file (in AnimateCC, go file -> new -> actionscript 3.0 class- Give it the name SaveData). Make the content of the file look like this:

    package {
        public class SaveData {
            public var userName:String;
            public var wrongAttempts:int;
            public var correctAttempts:int;
            public var timeToCompletion:Number;
        }
    }   

Add whatever properties you want to store as public vars as shown.

Then, register the class so it can saved in a shared object (you can do this at the start of your app)

flash.net.registerClassAlias("SaveData",SaveData);

Then create a global save data variable:

var saveData:SaveData;

And load in the data:

saveData = sharedObject.data.saveData as SaveData;
if(saveData){
    //load your saved data
}else{
    saveData = new SaveData();//create a new one since one didn't previously exist
}

During you app, assign values to the save data object:

saveData.wrongAttempts++;

Then later save it:

sharedObject.data.saveData = saveData;
sharedObject.flush();
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Here are some similar answers I've given that show other methods: https://stackoverflow.com/questions/15667848/as3-for-ios-how-to-serialize-an-array-and-then-save-it/15668079#15668079 – BadFeelingAboutThis Jul 24 '17 at 17:05
  • This one shows writing a file (instead of shared object) https://stackoverflow.com/questions/33422242/how-do-i-serialize-vector-customobject/33455756#33455756 – BadFeelingAboutThis Jul 24 '17 at 17:06
  • Or this one: https://stackoverflow.com/questions/30125221/using-file-to-save-scene-object-locations-to-rebuild-later-in-as3/30131304#30131304 – BadFeelingAboutThis Jul 24 '17 at 17:09
1

well one way is to have a sqlite database inside your Air app but it needs sql coding skills . http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118676a5497-7fb4.html

Amir Rasti
  • 706
  • 7
  • 19
  • 2
    Now please defend why storing a couple of strings and numbers not only needs SQL database, but also is "the best way". – Organis Jul 24 '17 at 15:58