Genesis John Dagdag
Genesis John Dagdag

Reputation: 23

How to create a save button in flash?

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.

Upvotes: 1

Views: 1691

Answers (1)

Marty
Marty

Reputation: 39456

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;
}

Upvotes: 1

Related Questions