Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website.  Home Flash & Swish Flash Tutorials SharedObject and You
Your Ad Here

SharedObject and You


Hi and welcome to this tutorial. This tutorial covers SharedObject. SharedObject is a kind of cookie for Flash. Though, SharedObject is much more powerful. SharedObject isn't just a way of saving mere strings on the client's hard disk, you can store texts, strings, numbers and even objects. For instance, if you want to save a date object in JavaScript you would have to go though a whole encoding process, with SharedObject you just save it, and be ready.

Starting with SharedObject

SharedObject is pretty simple to work with. First you need to call a saved object. This is done with the getLocal() function. getLocal is part of the SharedObject class and expects one parameter: the name of the object you want to call. If the object doesn't exist, it is created; now isn't that nifty. Anyway, the value that is returned by SharedObject.getLocal() is your cookie. It holds all the saved data and the functions you need to contol it. It's really easy to define variables in the sharedObject. All the data is held in the data object. So to define a variable all you have to do is define a variable in the data object. A little example:

myOjbect = SharedObject.getLocal("test"); //Get the 'test' SharedObject and save it in myObject myObject.data.
myName = "Mark"; //Save the value "Mark" in the myName variable in the SharedObject

Now that the variables are created, there is one more thing you need to know. When you edit the object, the SharedObject isn't saved. If Flash would do that, it would be very inefficient, and would take up a lot of system resources. That's why Flash only saves the SharedObject when you tell it to. You can do that with the flush() function. When you call this function, all the values are saved:

myOjbect = SharedObject.getLocal("test"); //Get the 'test' SharedObject and save it in myObject myObject.data.
myName = "Mark"; //Save the value "Mark" in the myName variable in the SharedObject
myObject.flush(); //Save the values

Now all the values are saved. Calling works in exactly the same way.

image 1
First run

image 2
Second run

Last little tips

You've now basicly grasped sharedObject. All that remains are a few little leads. First is that you can check if you've allready defined a variable or not by comparing it to undefined:

if (myOjbect.data.myName == undefined) { //Variable doesn't exist so define it... myObject.data.myName = "Mark"; } else { //Do stuff with the existing variable... trace(myObject.data.myName); }

Or saving objects to the sharedObject...:

myObject.data.myArray = new Array(); myObject.data.myObject = new Object(); myObject.data.myObject.myFunction = function () { trace("function called!"); }

The possibilities are endless :)



Author's URL: MicrOchip
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Internet & computing Flash is a vector-based moving graphics format created by Macromedia for the publication of animations on the World Wide Web. More Flash & Swish: Most Popular Materials | Fresh Materials | More Flash Tutorials at FlashPerfection.com

Add comments to "SharedObject and You"

Only registered users can write comment

No comments yet...