Search code examples
actionscript-3flashair

ActionScript3: Cannot save and load objects - AIR


I am trying to create functions to save and load objects. I am storing the objects in File.applicationStorageDirectory...and using File Streams. At some point I thought my code did work, and then it stopped soon after. It also saved the last string I inputed but none of the others. I have pastebinned the functions I think will be necessary, even if someone could point me in the right direction. I am aiming to publish this on an Apple iPad...

http://pastebin.com/61WLLUAB

I am in the process of learning to program, and appreciate any constructive criticism with my layout.

Thanks


Solution

  • I think flash.utils.registerClassAlias() and SharedObjects will be the key for loading/saving objects. Be sure to implement IExternalizable on all objects that will be processed while you save and load your game.

    About why does your function not work - first, you don't actually save your state between main sessions, you instead initialize all the objects anew, write them to files and then read them back, thus erasing previous data. You need to try reading an object out of file system prior to instantiating them anew, for each of the charID.IDs you have there. You don't have your IDs instantiated when your Main constructor starts, so Main doesn't know if any of the objects were saved beforehand.