-1

i cant figure out this one so maby you guys can help me out.

i store some data in the form of a array filled with Objects in this example cards.

in my main class i have the following code:

deckSprite.savedData = SharedObject.getLocal("cardsdata");
deckSprite.savedData.data.savedArray = deckSprite.deckArr;
deckSprite.savedData.flush();
trace(deckSprite.savedData.data.savedArray);

the trace will output something like [object card1, object card2, object card3]

now in a static class called "deckSprite" i have this:

savedData = sharedObject.getLocal("cardsdata");
if (savedData.data.savedArray == undefined)
{
trace("no save yet");
}
{
else
{
trace("save loaded");
deckArr = savedData.data.savedArray;
trace(savedData.data.savedArray);

now my trace data turns out only ", ," (somehow the cards are gone).

now after i got saved data i restart my application and whenever he tryes to acces the deckArr it crashes giving me the error "A term is undefined and has no properties".

how is it possible that when i save the array it saves all the cards inside the array and when i restart the application its suddenly only ",,"but the cards are gone?

  • You may find this question useful: http://stackoverflow.com/questions/30125221/using-file-to-save-scene-object-locations-to-rebuild-later-in-as3/30131304#30131304 – BadFeelingAboutThis Sep 22 '15 at 20:27

1 Answers1

0

When serializing objects in AS3, you have to register their class using registerClassAlias() from package flash.net. So you have to call something like

registerClassAlias('com.example.deck', Deck)

in the program before any saving or loading happens.

See full reference at AS3 API Reference


NOTE: As pointed out by @BadFeelingAboutThis in comments, you have to register all referenced class in your Deck, i.e. if your deck looks like this:

class Deck {
    var firstCard:Card;
    var type:DeckType;
}

to be able to save Deck to SharedObject you have to call

registerClassAlias('com.example.deck', Deck);
registerClassAlias('com.example.card', Card);
registerClassAlias('com.example.decktype', DeckType);

before any saving/loading is done.


EDIT

Everything depends on content of your array If I assume, your deckSprite is declared like this:

var deck1:Deck = new Deck();
var deck2:Deck = new Deck();
var deckArr:Array = new Array(deck1, deck2);
var deckSprite:DeckSprite = new DeckSprite()

deckSprite.setDeckArr(deckArr);

then before adding deckArray to SharedObject, you have to call registerClassAlias(). Sou your save code will look like this:

registerClassAlias('com.example.deck', Deck);
deckSprite.savedData = SharedObject.getLocal("cardsdata");
deckSprite.savedData.data.savedArray = deckSprite.deckArr;
deckSprite.savedData.flush();
trace(deckSprite.savedData.data.savedArray);

(replace the Deck with actual class you use for representing your decks)

Similarly the first line has to be called before you do any loading.

Of course it is best not to repeat yourself, so you should call registerClassAlias('com.example.deck', Deck); only once in your program, so for example in some init() method in your main class, if you have something like that.

Kejml
  • 2,114
  • 1
  • 21
  • 31
  • can you give me a example, cant seem to get it right – Dennis Castelijn Sep 22 '15 at 20:10
  • 1
    Might be worth pointing out as well, that you need to register every non-primitive class that is nested in your array, so your `card1` `card2` `card3` etc, plus any non-primitives that may be inside those card classes (if any). That all said, storing display objects in shared objects doesn't usually work this way. I'd check out this question: http://stackoverflow.com/questions/30125221/using-file-to-save-scene-object-locations-to-rebuild-later-in-as3/30131304#30131304 – BadFeelingAboutThis Sep 22 '15 at 20:23
  • @Denis see my updated answer and also see BadFeelingAboutThis's comment, he is absolutely right, I'll update the answer with that info – Kejml Sep 22 '15 at 20:26