Home » Code

Actionscript Locally Persistent Data Object

30 May 2009 2,473 views No Comment

Flash has an easy way to save locally persistent data directly from Actionscript. Using the SharedObject Class, one can define many data points within the same object and use them like you would “cookies”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Declare a cookie name
var cookieName:String = "cookieExample";
 
//Create an object to store cookie data
var cookie:SharedObject = SharedObject.getLocal(cookieName);
 
//If the data we are looking for doesn't exist (and it truly doesn't exist, it's just not 0)
if(!cookie.data.firstPageToShow && cookie.data.firstPageToShow!=0){
 
	//initiate the data point
	cookie.data.firstPageToShow = 0;
 
}else{
 
	//increment the data point
	cookie.data.firstPageToShow += 1;
}

After this code runs, if the user has visited the site before, they will be taken to the page after they last left, or if they haven’t visited the site yet, the user will be taken to the 0th page.

In the real world, this code has been used to ensure that users visiting a site with a rotating campaign, saw each image and caption of the campaign on subsequent visits.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.