Search tutorials
Using Shared Objects to store/load data.
For this tutorial I expect that you have at least a basic knowledge of ActionScript and Flash in general. In this tutorial I'll explain how to store data, like text, coordinates of a movie clip, or whatever, in a SharedObject. This is pretty useful if you made a game too long and you want the user to save its game and load it anothe time when he wants to continue playing, or to store data to remember certain users, etc. I'm sure you will find a lot of situations tu use this.
Let's do the App:
This app will have 1 input text, 2 dynamic texts, 1 movie clip and 2 buttons to save and load.
First we store the inicial coordinates of the movieClip "mclip" in i_x and i_y, this bit of code goes in the first frame of te movie:
i_x = _root.mclip._x; i_y = _root.mclip._y;1. This is the code for the Save button:
on (release) {
var my_so:SharedObject = SharedObject.getLocal("savedtext", "/");
my_so.data.txt = _root.txtbox;
my_so.data.mc_x = _root.mclip._x;
my_so.data.mc_y = _root.mclip._y;
my_so.flush();
//
_root.txt = "";
_root.d2 = "";
_root.d2 = "";
_root.mclip._x = i_x;
_root.mclip._y = i_y;
}
1.1 Explanation for Save button:First we declare the "my_so" variable:
"var my_so:SharedObject = SharedObject.getLocal("savedtext", "/");"":SharedObject"
means that the my_so variable is a SharedObject type variable. In
"SharedObject.getLocal("savedtext", "/")" we create the coockie with the name
"savedtext" and in the root directory of the server ("/").Then we store all the data we want. The way to do this is "my_so.data.var = something", where var is the name of a variable - that will contain what goes after the "="- that you will call later to load. In the third line of code we store the text of a textbox (_root.txtbox) in the variable txt of my_so, and in the forth and fifth lines we stored the x and y coordinates of a movie clip named mclip in the mc_x and mc_y variables of my_so.
The rest of the lines (afret the // comment line) are optional, they are just to reset the textboxes and return the movie clip to its original position.
2. Now, the load button:
on (release) {
var my_so:SharedObject = SharedObject.getLocal("savedtext", "/");
_root.txt = my_so.data.txt;
_root.mclip._x = my_so.data.mc_x;
_root.mclip._y = my_so.data.mc_y;
_root.d2 = my_so.data.mc_x
_root.d3 = my_so.data.mc_y
}
2.1 Explanation:first we load the my_so cookie the same way we created it in the save button, this is to asure that if the user closes the window after saving and then reopens it, he'll get the info he stored from the right cookie (named "savedtext"). the rest of the lines loads the data from my_so calling each data by it's variable name, into the corresponding textbox. Notice that this is the inverse way to save, so we are assigning the my_so data to the objects.
3 Finishing:
Note: This is optional, if you want to have this program functional the way i did, make this, otherwise, apply the knowledge you've learned here in your own application.
on (press) {
this.startDrag();
}
on (release) {
this.stopDrag();
}
This code is fore the button inside the movie clip "mclip", so you make it
draggable to test this program. ---------- Well, thats it, it's very simple. I
hope you like this! Click here to see the final
product working. | » Level Intermediate |
|
Added: 2005-07-27 Rating: 7 Votes: 4 |
| » Author |
| Web Designer, Graphic Designer |
| » Download |
| Download the files used in this tutorial. |
| Download (10 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!