1

I have a game and I used Flash cs5 ActionScript3. I want to create a save button for the game. When the player loads the game, the saved game will open. Thanks.

1 Answers1

1

Look at the SharedObject class; particularly its data property.

Essentially:

  1. Define a SharedObject like so:

    var saveData:SharedObject = SharedObject.getLocal("MyGame");

  2. Use the data property to store information which will be accessible the next time you open the application. It's important to note that the data will only be retained if the .swf remains in the same location, either on the web or locally.

    if(!saveData.data.test)
        saveData.data.test = "Test string";

  3. You'll be able to access the information you've stored in the data object as expected:

    trace(saveData.data.test); // Test string

The idea is that initially you'll create all of the properties that you may want to save when the game launches for the first time:

if(!saveData.data.ready)
{
    saveData.data.ready = true;

    saveData.data.playerHealth = 100;
    saveData.data.levelsUnlocked = 1;
}

And when you hit "save", overwrite these properties:

function save(so:SharedObject):void
{
    so.data.playerHealth = player.health;
    so.data.levelsUnlocked = currentLevel;
}
Marty
  • 39,033
  • 19
  • 93
  • 162
  • Thanks @Marty Wallace. How about saving the current timeline. For example, when I played the flash file, it stopped at frame 5. Then if I want to load the flash file again, it should be at frame 5. How can I do this? Thanks. – Genesis John Dagdag Feb 14 '12 at 14:29
  • So something like saveData.data.frame = currentFrame; and then when you load the game: gotoAndStop(saveData.data.frame); – Marty Feb 14 '12 at 20:59
  • Sir @Marty Wallace. I have this type of error during saving. Test string TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@300ba0e1 to flash.net.SharedObject. What do you think is the problem sir? – Genesis John Dagdag Feb 21 '12 at 02:26
  • @GenesisJohnDagdag Somewhere you're trying to cast an instance of `MouseEvent` to `SharedObject`. I suggest posting another question with relevant code so I can better help you. – Marty Feb 21 '12 at 02:29