First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Paul Flashdaweb | Website: http://flashdaweb.com

Here is my first tutorial at FlashDaWeb.
I will show how SharedObject might be used.

For illustration, I came up with an idea to show users the time they spend browsing a particular page.

Here is an example.

Open this demo page and see the timer, then close it and open again anytime you want. Notice, time will not start again from 0:0, it will continue counting from the very values you previously closed it. Quite nice, isn’t it? :)

In your hands.

The above functionality was implemented with Adobe Flash CS3, ActionScript 3 and SharedObject.

You may download the sources .fla and .as files

The power of shared objects.

For this tutorial I assume you already know the basics of AS3 and are quite acquainted with Flash CS IDE.

I created a new document and added new movie clip. It’s named “mainObject“. I put one instance of it at stage and linked it to the actionscript class SharedObjectReader.as

In our class, we have a member of shared object:

private var _sharedObj:SharedObject;

In constructor of our class, we create children and start actionscript timer-based cycle using setInterval.

public function SharedObjectReader ()

//load and update the correct values

//update the values every second

In onEverySec method we bind our _sharedObj to a local instance with static method getLocal(), put it in try-catch block to handle the exceptions.
TotalTimeAtThisPage is a name of Shared Object, you may use anything in there.

_sharedObj = SharedObject.getLocal(”TotalTimeAtThisPage”);
catch (error:Error)
trace(”SharedObject Error:”+error.toString());

When having initialized shared object, we read the values from it, accessing them via “data” member

//read the values, if user had already been here, he has them already.
_secs = _sharedObj.data.seconds;
_mins = _sharedObj.data.minutes;

Note, “seconds” and “minutes” are my own variable names, you may use any of them here, but remember you should use the same names when saving the values.

Then, we increase the values, format them and update the text displayed. And then store updated values in the same shared object member _sharedObj.

//store the values
_sharedObj.data.seconds = _secs;
_sharedObj.data.minutes = _mins;

Call flush() to save them to hard disk

//saving the values

Finish the iteration with close() method calling. As I have described in ActionScript 3, SharedObject crashing Flash CS IDE” it is a must to call this method as it may cause a crash of Flash IDE or other problems.


Well, that’s it. SharedObjects are not that tough part of ActionScript, but it’s very helpful.

» Level Intermediate

Added: 2008-01-29
Rating: 4 Votes: 4
» Author
Experienced ActionScript Developer
» 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.