Wednesday, April 9, 2008

Flash Shared Object (aka Flash Cookies)

This will enable you to create a variable and have flash remember it when the page is reloaded. You can even do it across multiple swfs. This code is for a flash scavenger hunt I made, where I had to keep track of which easter eggs had been found, and which easter egg to point the user to next. It also has to be able to know when all 3 have been found:

the_so=SharedObject.getLocal("scavenger","/"); //the "/" makes it so it works across multiple swf's
// set the new variables to 0 by default
if (the_so.data._sumoTask == undefined) {
the_so.data._sumoTask=0;
the_so.flush; //this saves the shared object immediately as opposed to when the swf shuts down
}
if (the_so.data._computerTask == undefined) {
the_so.data._computerTask=0;
the_so.flush;
}

if (the_so.data._vanTask == undefined) {
the_so.data._vanTask=0;
the_so.flush;
}


//The following sets up variables to match the shared object variables
sumoTask = the_so.data._sumoTask;
computerTask = the_so.data._computerTask;
vanTask = the_so.data._vanTask;

vanMorph_mc.button_btn.onRelease = function(){
vanMorph_mc.gotoAndPlay("morph");
the_so.data._vanTask = 1; //sets this so variable to 1. now when you come back or go to another task, it knows this task has already been completed.
}

clear_btn.onRelease = function(){
the_so.clear(); //This will clear the cookies
}

You can make any variable you want to remember a number of different things...such as scores, backgrounds (if you have multiple bg, you can remember which one the user had selected), etc.

No comments: