First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: The_Nephilim

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;
	_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) {
on (release) {
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: 5.8 Votes: 5
» Author
Web Designer, Graphic Designer
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.