Flashadvisor logo
:: Desktop Shortcut
:: Flash Help
Advices from Experts

 Submit Here! Link to Us Newsletter Tell a Friend

      Add Tutorial |

 

Advertise here

Create Speaking Characters for your website and Flash movies. 15 Day Free Trial




PHP Scripts

 

 Home > Tutorials  > ActionScript

how SharedObject might be used

Author: Paul | Email
Website : flashdaweb.com


Advertisement

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, is not 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 is 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 ()
{
createChildren();

//load and update the correct values
onEverySec();

//update the values every second
setInterval(onEverySec,1000);
}

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.

try
{
_sharedObj = SharedObject.getLocal("TotalTimeAtThisPage");
}
catch (error:Error)
{
trace("SharedObject Error:"+error.toString());
return;
}

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
_sharedObj.flush();


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.


_sharedObj.close();


Well, that is it. SharedObjects are not that tough part of ActionScript, but it is very helpful.
Download Source File

| 1 |

Rate This Tutorial :
We hope the information helped you. If you have any questions or comments, please don't hesitate to post them on the Forums section
Submit your Tutorial at Click Here
  

Home | About Us | Contact Us | Feedback | Advertise with us

Best viewed in 800x600 resolution with Internet Explorer.

Site Developed and Hosted by  ethicsolutions.com
All rights reserved with FlashAdvisor

Flash is a registered trademark of Adobe Systems, Inc.

5 users online