Flash Vista - Home
Newest Cool Site
The Two Tales
Site info | Archive
Main Navigation
Home
New Links
Top Rated
Most Popular
Cool Sites
Search

Flash Tutorials
Flash Resources
Flash News
Flash Templates
Flash Intro Templates
Website Templates
Flash Games
Flash Books
FlashVista Polls
Sitemap


Random Link
English
Polydor UK
Site info | Get another
FlashVista
Login
Register
Subscribe

Add Link
Modify Link
Favorites
Suggest Category

Advertise with us
Support us
Credits / Thanks
Contact

Flash Templates:

More Templates ...

Mailing List
User

Password



Register
Forgot password?
Partner websites
iPhone cases and accessories
Free Seo Tools
Free Tutorials
Free Video Tutorials
Best Free Scripts
Wii Fit
Wii Fit News
Other Resources
Suggest
Suggest this site to a friend


Mailing List
Status: Not logged in

English English German French Spanish Italian Portuguese Russian Polish Finnish Dutch Swedish Thai Romanian Traditional Chinese Simplified Chinese
SearchNot logged in
Keyword: Search for: Advanced Search


Using Local Shared Objects in Flash MX: The Flash Cookies


NEW Flash Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, flash 8 and more.


Download the source file for this tutorial Printer version



by Philipp Kostin
www.philarmon.com


Before Flash MX, it was pretty tricky to "remeber" the data in a Flash movie - it could be done with a standard browser cookie, which was hard to implement for someone with intermediate Flash skills, using a 3rd party script such as PHP or ASP or (in offline Flash applications) with the undocumented fscommand "save". Flash MX's Shared objects allow to store and retrieve the information within a Flash movie easily. Lets see how it works.


What is a local shared object ?

Flash MX Shared objects is a new feature allow you to store information at the user's machine the same way as cookies would and retreive it at a later time. Shared objects could be used to remember the user's name, the number of the level he last played in a game, his highscore or anything else you can imagine.

Shared objects are stored in .sol files located in the Flash player directory of the user's profile : "C:/Documents and Settings/Administrator/Application Data/Macromedia/Flash Player", and have their own format

Here is a working example of a movie using the Shared Object. Type in your name and age and click on "Save". Then, refresh this page to let Flash read and display the stored data:
WIDTH="200" HEIGHT="150" id="shared_object" ALIGN="">
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">



Creating a shared object

First, we have to create a local shared object within a Flash movie. To do so, just put this line of code in the first frame of your movie:


local_data = SharedObject.getLocal("user_data");


Now we have created an Object named "local_data" which is associated with a shared object on the local hard-drive named user_data. Note that in the feature, this data can be read from other movies from the same domain that created the Shared Object.


Writing data

Lets store something in our fresh created Shared Object. Lets say, we want to store the user's name and his age in it. To do so, use this:


local_data.data.user_name = "John Smith";
local_data.data.user_age = 23;
local_data.flush ()


Note that this code must be in the same level where you have created the Shared Object.

The flush () command is optional, it is used to write the information to disk immediately. If you don't use this command, Flash MX writes the shared object to a file when the SWF movie is closed or when the shared object is garbage-collected because it no longer has any references to it.


Reading data

To retrieve data from a saved Shared Object, just use the following syntax:


stored_user_name = local_data.data.user_name;
stored_user_age = local_data.data.user_age;


Don't forget that you still have to create the local_data shared object first. Now, the user's name is stored in a variable called "stored_user_name" and the user's age in the variable "stored_user_age"and you can use it anywhere in your movie.

You can store more information in one Shared Object file in the same way.

Similar to the way how we have saved simple text variables you can store whole arrays and other objects.


Important notices

There are some important things to remember when using the local Shared Objects in your movies:

  1. The primary drawback of shared objects is that it can be overwritten/disabled/erased by the user (right click on the swf, and click on settings), so avoid to store information which is absolutely necessary to let the application work properly. Think of it as an additional feature.




  2. The amount of the information you can store in a local Shared Object from one domain is set to 100 kb by default. If you will try to store more information, the Flash player will ask the user for a permission to increase this limit. Be sure the stage of your movie is at least this is the minimum size Macromedia Flash MX requires to display the dialog box.




Additional information

This tutorial covers the basics of the local Shared Object feature. You can read more about it and take a look at additional features at the Macromedia website:

What is a Shared Object ?
Using local shared objects in Macromedia Flash MX


(Added: 01-19-2004, Hits: 1, Rating: 3.48, Votes: 25, Reviews: 12)
Add to Favorites Suggest to a Friend

Reviews: (10)

can you please explaithis in more detail, i can't get it to work at all...

can youplease post where i need to put the script, like on a button, or on the frame containing the text boxes to load the data into...

i really appreciate any help, since my text boxes dont change no matter what i try.

(Added: 02-23-2007 User: Guest)

how do you save tings like x and y values of an object... eg: if the user drags an object around the stage and then drops it, how do you save the x and y positions so they can refresh the page and the object automatically moves to the position they dragged it to before?
(Added: 02-22-2007 User: Guest)

i would like to know how use LSO to get user name and then read that user name on later login to let that user continue from the last frame he stopped at b4 closing the application and to save his scores in a quiz and do the LSO do overwrite each other if another user used the same CD or it append it to let more than one user use the same application and call the scores and last point he wast at for each user independently
(Added: 02-19-2007 User: doaa)

How can i aplly this so that when my flash movie plays on one page, when i go to another page the cookie can control the timeline of the swf. I have a header with links and i don't want the intro to play on every page just the index, but the swf contains all my navigation.
thanks

(Added: 05-18-2006 User: Guest)

td8f4

thanks, but how would i make it so it ca save a game?

thanks

td8f4@hotmail.com

(Added: 04-27-2006 User: Guest)

That was all well explained and detailed, but there is one thing about all this... one thing about every Shared Object tutorial I've looked at today!
Nobody explains how to save variables! I don't need to save text, I want to save numbers! Not written numbers, numbers you can add and subtract from.
For example, on the first frame of the timeline, writing this:

_root.examplevariable = 0;

Which defines 'examplevariable' as being equal to zero. Then I have buttons in my example Flash file which might have a script like:

on (release) {
_root.examplevariable +=1;
}

Which adds 1 to the variable.

The variable I want to save is an active one, which is always changing.

So please contact me if you know how to do such a thing at danny.poloskei@gmail.com

(Added: 01-15-2006 User: Guest)

Zbotic, you can use the shared object in any flash movie from the same domain
(Added: 07-31-2005 User: Philarmon )

Thank you so much for the lesson. I do have a question: What if you want a different flash file on your site to use the shared object?
(Added: 07-28-2005 User: zbotic )

Very Nice
But When instal new windows what I doing

(Added: 05-30-2005 User: Guest)

vamos fazer um teste para ver se funciona mesmo
(Added: 05-03-2005 User: Guest)


Add Review
Please note:
We review EVERY comment before it appears on the site, so please dont waste your time by posting spam links :)
No URLs allowed, no HTML please.

If you register or login first, your review will contain your nickname


Rate It



Excellent!
Very Good
Good
Fair
Poor