ActionScript 3.0: how to use SharedObject
I’ve been using SharedObject a lot lately, Flash’s own version of a cookie.
and so I thought it’d be about time telling a bit about how this works.
so I’ve made this very simple example, that works as a counter, telling you how many time you’ve visited a certain webpage.
check out the simple counter built with SharedObject here:
reload the page, to see how the SharedObject tells you how many times you’ve reloaded it.
the motor behind this is kinda simple.
here’s all the actionscript:
//create a shared object or load it
var sharedObject:SharedObject = SharedObject.getLocal("firstTimeOrNot");
//if the sharedObject is only just created, the min_variabel will be equal undefined and we set it to 0
//if it exists we just add 1 to it, as loading it counts as another visit to the webpage
if (sharedObject.data.min_variabel == undefined){
sharedObject.data.min_variabel = 0;
sharedObject.flush();
} else {
sharedObject.data.min_variabel++;
sharedObject.flush();
}
//the .flush() makes sure, that the changes of the min_variabel is immediately saved to the sharedObject
//set the dynamic text equal to the min_variabel
numberOfVisits.text = sharedObject.data.min_variabel
var sharedObject:SharedObject = SharedObject.getLocal("firstTimeOrNot");
//if the sharedObject is only just created, the min_variabel will be equal undefined and we set it to 0
//if it exists we just add 1 to it, as loading it counts as another visit to the webpage
if (sharedObject.data.min_variabel == undefined){
sharedObject.data.min_variabel = 0;
sharedObject.flush();
} else {
sharedObject.data.min_variabel++;
sharedObject.flush();
}
//the .flush() makes sure, that the changes of the min_variabel is immediately saved to the sharedObject
//set the dynamic text equal to the min_variabel
numberOfVisits.text = sharedObject.data.min_variabel
questions?
just post them below
Hi, I’ve been trying to get shared objects to work for a long time. I just found this and tried copy-pasting your code in a flash file and it worked. I also tried to find what I have done wrong, and kinda failed…
Could you help me? Here’s my code:
var sharedObject:SharedObject = SharedObject.getLocal(“ftd”);
if (sharedObject.data.played != undefined) {
_quality = sharedObject.data.setQUALITY;
hud = sharedObject.data.setHUD;
helpers = sharedObject.data.setHELP;
} else {
_quality = “low”;
//Init save vars…
_root.loadingInfo.replaceText(0,1000,”LOADING: CREATING GAME”);
sharedObject.data.played = new Boolean(true);
sharedObject.data.setQUALITY = new String(_quality);
sharedObject.data.setHUD = new Boolean(hud);
sharedObject.data.setHELP = new Boolean(helpers);
error = sharedObject.data.played == false;
if (sharedObject.data.played != true) {
_root.loadingInfo.replaceText(0,1000,”ERROR: CANNOT USE SHAREDOBJECTS – CANNOT PLAY”);
}
if (sharedObject.flush() != true) {
_root.loadingInfo.replaceText(0,1000,”ERROR: WILL NOT SAVE – CANNOT PLAY”);
error = true;
lbar.stop();
}
}
Thank you!
-Sam
Sam
19 jul 08 at 19:19
Oh, I think I found my mistake. Please tell me if I’m wrong (but it works now! *happy*); When setting variables in sharedObject.data you cannot call the new method. You have to just put the value, i.e.
sharedObject.data.X = 3;
instead of
sharedObject.data.X = new Number(3);
Thanks again!
-Sam
Sam
19 jul 08 at 19:24
THANK YOU SO MUCH!!!!!!!!!!!
I have been looking for this forever, and you finally helped solve the problem that I had with my “sol experimentations”.
Jeff Simms
10 maj 09 at 18:25
hey Jeff.
nice to hear you could use it :O)
happy flashin’
felisan
admin
10 maj 09 at 21:17