Date display area
Local memory in ActionScript
Persistent Memory in Flash – an overview
Flash has a number if ways that it can implement persistent memory, but the easiest is to use the SharedObject class. This class automatically creates a file saved on the local computer – because of flash security restrictions, the user has no control over where it is solved and it is not readable by any other programme.

The SharedObject is quite a complex class because it also allows shared memory between programmes on different computers using a server running the Flash communication Sever software. We will wrap it in a simple custom class to hide this complexity.

Here is the UML diagram for the wrapper class:

There are no public properties and only 3 functions. We set up the memory and than save and get data. We will use this first to implement a fragment of an application that remembers its user’s name; the first time it is run, it asks for the name and then greets the user; on subsequent launches, it jumps straight to the greeting.


Example 1 – Remembering the user (a persistent greeter)
This example implements the LocalMemory class described above to produce an application that remembers its user from one session to another. We will follow good practice in laying out the timeline and start storing code in .as files.
  1. Make a new movie and create 3 extra layers, naming them scripts, labels, input and text field.
  2. Select frame 10 in all of your layers and then choose Blank Keyframe from the Insert menu.
  3. Do the same for frame 20 and 30.
  4. Select Frame 1 of the Labels layer and then, using the Frame palette, give it the label init.
  5. Select frame 10 of the Labels layer and give that the label name_enter.
  6. Select frame 20 of the Labels layer and give that the label greet.
  7. Lock the scripts and labels layers. Your timeline should look like this:
  8. Save your movie as so_greeter.fla in a new project folder.
  9. Select the text field layer at frame 10 (name_enter).
  10. Select the text tool and drag out a text box on stage.
  11. In the properties panel, set your text field options as follows:
  12. Make sure your text field instance is named name_txt.
  13. You now need to make a button that the user will press to save their name.
  14. When you’ve made your symbol, select the input layer at frame 10, and drag your symbol onstage to create an instance
  15. Name your button instance saveName_btn.
  16. Select the text field layer at frame 20.
  17. Choose the text tool and drag out a large textbox. Set its properties like this:
  18. Choose New from the file menu and then select ActionScript File. This opens a new .as external script file – the only place you are allowed to create classes.
  19. Save your blank file as LocalMemory.as inside a new folder called memory which is inside your project folder.
  20. Carefully type the following: -
  21. package memory { import flash.net.SharedObject; public class LocalMemory{ private var myMem:SharedObject=null; // the constructor function //send it the name for the memory object public function LocalMemory(memName:String){ // set up or load a persistent local object myMem=SharedObject.getLocal(memName); } public function saveData(myKey:String,myData:*):void { // attach the data using the key as the property name myMem.data[myKey]=myData; // save to disc myMem.flush(); } public function getData(key:String):*{ if (myMem.data.hasOwnProperty(key)){ return myMem.data[key]; } else { trace("Local Memory: no stored data for "+key); return null; } } } }
  22. Select the actions layer in frame 1 and open the actions panel.
  23. Type the following:
  24. import memory.LocalMemory; var myMemory:LocalMemory = new LocalMemory("userNameExample"); if (myMemory.getData("userName")==null){ // no stored name gotoAndPlay("name_enter"); } else { // stored name exists gotoAndPlay("greet"); } stop();
  25. Select frame 10 (name_enter) of the scripts layer. Type as follows:
  26. import memory.LocalMemory; var myMemory:LocalMemory = new LocalMemory("userNameExample"); if (myMemory.getData("userName")==null){ // no stored name gotoAndPlay("name_enter"); } else { // stored name exists gotoAndPlay("greet"); } stop();
  27. Select the actions layer at frame 20 (greet), type:
  28. import memory.LocalMemory; // script for greet this.greet_txt.text="Welcome "+myMemory.getData("userName"); stop();
  29. Check your code syntax and close the Actions pane. Your movie is finished.
  30. Test it by pressing the CONTROL and ENTER keys together. Note what happens.
  31. Close the movie and then run it again.
Further developments
  • Make a movie that allows the user to change their screen name via a button on the greet screen that takes them back to the name_enter section.
  • Can you make the programme ask for, and store, more information about the user?

This page was last updated on 31st October 2013