Skip to main content
Known Participant
June 9, 2011
Question

Urgent, SharedObject not working on iPad 2?

  • June 9, 2011
  • 1 reply
  • 3987 views

Hello,

One of my iPad games uses SharedObject to store data. The game was just released on the AppStore and I am now getting reports from players with iPad 2 that this is not working (data isn't saved). The game works great on iPad 1 but unfortunately I was not able to test it on iPad 2 before submitting it to the AppStore.

Have any of you had any issues with SharedObject on iPad 2?

Is there any other way to store data instead of SharedObject?

Thanks in advance. It's really frustrating to have a broken game in the AppStore.

Best,

Andreas

This topic has been closed for replies.

1 reply

Participating Frequently
June 9, 2011

Hi Andreas,

#1

you have in theory unlimited access to application documents directory, so you could write data there using File/FileStream

#2

both SharedObject and File/FileStream are advised solution to flush application state

see:

http://www.adobe.com/devnet/flash/articles/saving_state_air_apps.html

Can you post on what event(s) your application is supposed to save its state (write data)? Maybe that would give some clue(s) on possible reason/explanation.

regards,

Peter

RehnbergAuthor
Known Participant
June 9, 2011

Thanks for the reply Peter.

I have heard from a few players that reinstalling the app has fixed their problem. So it seems like they get a corrupt Shared Object the first time they play it?

The game is a quite simple hangman game, so the saving is done after each turn a character is chosen.

The shared object is set at startup:

so = SharedObject.getLocal("HMgameAR");

When the game has initiated, I check the current game data:

private function checkCurrentGame():void {

     if (so.data['gamePart'] == undefined) {

          so.data['gamePart'] = "startMenu";

     }

     if (so.data['topScore'] == undefined) {

          so.data['topScore'] = 0;

     }

     if (so.data['dictionaryPos'] == undefined) {

          so.data['dictionaryPos'] = 0;

     }

     if (so.data['dictionary'] == undefined) {

          so.data['dictionary'] = getRandomWordArray();

          // this is an array with numbers from 1 to 400

          // (numbers of words in the dictionary)

     }

     // do game specific updates

     var tGamePart:String = so.data['gamePart'];

     if (tGamePart == "startMenu") { return; }

     if (tGamePart == "selectLevel") { continue_selectLevel(); }

     else if (tGamePart == "guessWord") { continue_guessWord(); }

     else if (tGamePart == "writeWord") { continue_writeWord(); }

}

The state is saved each time a player is guessing a character:

public function so_guessCharacter():void {

     so.data['gamePart']    = "guessWord";

     so.data['gameVer']     = gameVer;

     so.data['gameLevel']   = gameLevel;

     so.data['secretWord']  = secretWord;

     so.data['lettersLeft'] = lettersLeft;

     so.data['turn']        = turn;

     so.data['tries']      = tries;

     so.data['guessArray']  = guessArray;

     so.data['score1']      = score1;

     so.data['score2']      = score2;

     so.data['status']      = status;

}

Also after each round, this function is run to get the next secret word (a number for the dictionary array):

private function getNextWordNr():int {

     // get current position and add 1

     var tNr:int = so.data['dictionaryPos'];

     tNr++;

     // randomize new dictionary if array is done

     if (tNr > 399){

          so.data['dictionary'] = getRandomWordArray();

          tNr = 0;

     }

     so.data['dictionaryPos'] = so.data['dictionary'][tNr];

     return so.data['dictionaryPos'];

}

It's the getNextWordNr() function that is causing the worst problems, since it returns 0 for the players without a working Shared Object, so they get the same secret word every time they play...

As you see I am not using the flush() function, could that be the solution? It's a bit tricky when I can't reproduce the problem. And since I will have to wait about a week for a new version to be approved by Apple I need to make sure it's fail-proof this time..

I appreciate your help!

Best,

Andreas

Inspiring
June 10, 2011

Hi Andreas,

You need to call the flush() to save your lso.

It is particular important to do it right away on mobile because you don't know when the user may exit your application or the system may shut it down if it runs out of memory.

You can set a listener to track the event as follows:

import flash.net.SharedObjectFlushStatus;

import flash.events.NetStatusEvent;


import flash.net.SharedObject;

var so:SharedObject = SharedObject.getLocal("myApplication");

so.data.animal = "Hamster";

so.data.food = ["Grains", "Avocado", "Carrot"];

so.data.isVegetarian = true;

so.data.stuff = {toy:"Wheel", house:"Cage"};


var flushStatus:String = so.flush();

if (flushStatus != null) {

     switch(flushStatus) {

          case SharedObjectFlushStatus.PENDING:

          so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);

          break;

     case SharedObjectFlushStatus.FLUSHED:

          trace("success");

          break;

     }

}


function onFlushStatus(event:NetStatusEvent):void {

     trace(event.info.code);

}

I hope this is helpful.

p.s. This is an example from my book on developing AIR apps for Android (and iOS for the most part) :-)

http://oreilly.com/catalog/0636920013884