At some point, you would like to store some data on the client side, say for example, to know if it’s the first time a user is accessing your Flash application/animation, and show him/her different content based on the data.
Just like a Web Cookie, Flash Cookies can be used. The best or evil part of it, is that Flash cookies are harder to remove. In fact, they don’t go away even if the client clears all his browser cookies. That’s because it’s the Flash Player that manages those cookies obviously.
Making these cookies is super easy:
add the import: import flash.net.SharedObject;
create your cookie object (SharedObject): var cookie:SharedObject = SharedObject.getLocal(“myFlashCookie”);
The handy thing about it, is that you can add any kind of info in it (Note that the amount of data that you can store is limited to the size that the user has chosen in the configuration of his Flash Player, normally that’s 100Kb).
Here are some handy things to know:
cookie.size : See if our cookie contains anything ( 0 means we have a fresh cookie… mmmh fresh cookie…. )
cookie.data : Array containing any object that we want (please not the size limitation described above)
cookie.data.VariableName = object : we can add any variable name to .data.
cookie.data.VariableName : if VariableName exists, it will return the object it is containing
cookie.flush() : save our cookie immediately to a file (otherwise, our cookie only exists in memory during the length of the session and not being stored on the clients machine)
cookie.clear() : deletes/clears the cookie ( :( )
Another useful thing to know is that you can call the cookie remotely.
Here’s a live example that I use, I want to show the user a tutorial on how to use my application. The tutorial will only be show when the cookie is empty.
import flash.net.SharedObject;
public class ComicGenerator extends MovieClip {
//Our Flash Cookie
var cookie:SharedObject = SharedObject.getLocal(“ComicBookGenerator”);
var forceTutorial:Boolean = false;
//Constructor
public function ComicGenerator() {
super();
initialiseStage();
//Force Cookie Deletion
if (forceTutorial) cookie.clear();
//Is this the first time the user sees our app?
if (cookie.size == 0) {
showTutorial();
cookie.data.FirstTime = true;
}
else {
showOperatingMode();
}
}
…
For more information, you can go take a look at the API documentation: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/S