AS3: How to use Flash Cookies


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:

  1. add the import:   import flash.net.SharedObject;
  2. 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/SharedObject.html

Advertisements

4 thoughts on “AS3: How to use Flash Cookies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s