Professional Documents
Culture Documents
One thing you want to do as a game developer is make your players want to
come back to your game more than once. And one of the best ways to
accomplish this is to enable them to save their data for persistent gameplay.
You might expect saving and loading to be tough to implement, but
actionscript 3 is helpful enough to include the powerfulSharedObject, which
makes saving and loading data a piece of cake.
In this tutorial, we will create the following demo. Click the +1 button a few
times to add to your score. If you refresh this webpage without clicking the
save button, your score will revert to 0 when the page loads. But if you do
click the save button, your score will load automatically the next time you
load the page. Try it out!
Before we can start using the shared object, we need to create it. Declare the
variable the same way you would any other but dont give it a value yet.
var saveDataObject:SharedObject;
(Note: You can combine this with the previous step and just write var
saveDataObject:SharedObject = SharedObject.getLocal("test"). The only reason I
divided into two sections is for clarity.)
The cool thing is that the data Object is built into a SharedObject, so all you
need to type to save something is:
saveDataObject.data.savedScore = currentScore;
Notice, you dont even need to define the savedScore property of the data
Object before you can set it to a value.
Testing it out:
Time to go from theory to a practical test. Our test project will include a
variable that keeps track of the players score. Clicking a button will add one
point and update the text field to display the current score. Clicking the save
button will save the current score to a shared object, and flush the data to the
players local drive.
Setting Up
Create a new Flash Professional project. We are going to set up the stage so
that it looks like the following screenshot:
First use the rectangle tool to create two buttons. It doesnt matter too much
what they look like, as long as you label them so you can tell them apart.
Next, I converted them one at a time into Symbols of the Button type (Modify
> Convert to Symbol). Name one of them btnAdd and the other btnSave.
Then, for simplicitys sake, give each button an instance name identical to its
symbol name (btnAdd and btnSave).
Because this is just a test, it doesnt really matter how the buttons look.
However, I did spend a little time adding some effects to the buttons so that
they change color when the player hovers their mouse cursor over the buttons,
or clicks on them. It doesnt require any code at all, so if youre curious to see
exactly what I did, just check out my source file (all you need to do is add more
frames to the Button Symbol, like in the image below).
After your buttons are set up and code-ready, create a text field above them
(using the Text Tool) and type Score: 0 inside it. Make sure to make it
a Dynamic Text Field (by using the drop-down list in the Properties panel).
This will allow you to give it an instance name, and therefore modify it with
code. Give it the instance name txtCode.
Time to Code
OK, open up your Actions panel for frame 1 of your main project timeline.
Start by declaring two variables: the currentScore, which will keep track of
how much score to display in the txtScore field, and the saveDataObject,
which is our Shared Object, and the focus of the tutorial.
1
2
var saveDataObject:SharedObject;
var currentScore:int;
Next, we need to create the init() function, and call it once to initialize the
project. Ive included everything in this function that it will include in the final
version, so dont be startled if you see something in there that we havent even
discussed yet. Ive commented everything, explaining as you read along. Ill
also explain afterwards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
theaddScore function when it is clicked, and the btnSave button will call
the saveDatafunction.
The next little section of code might look a little more complicated, but all it
does is check whether or not there is previous save data on the
computer. How can we figure out whether there is save data or not? Well,
one way is to check if our savedScoreproperty in the shared objects data
is null. If we have saved data previously, that data will have a value other
than null, so the conditional will be false, and well call theloadData function.
But if we never set the savedData to be anything, it will be null, so we wont
load the data. Instead, well just start by setting the score to 0.
Finally, we call a function to update the txtScore field.
Here are the 4 other functions which we reference in the initialization. This is
the entire remainder of the code in our demo program. You can add these
functions anywhere I just put them beneath the init function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function addScore(e:MouseEvent):void{
currentScore += 1; // add 1 to the score
updateScoreText(); // update the textfield
}
function saveData(e:MouseEvent):void{
saveDataObject.data.savedScore = currentScore; // set the saved score to the cur
trace("Data Saved!");
saveDataObject.flush(); // immediately save to the local drive
trace(saveDataObject.size); // this will show the size of the save file, in byte
}
function loadData():void{
currentScore = saveDataObject.data.savedScore; // set the current score to the s
trace("Data Loaded!");
}
function updateScoreText():void
{
txtScore.text = ("Score: " + currentScore); // set the text property of the txtS
trace("Score text updated");
}