The AS2 script: Memory.as

We now continue and look at the AS2 script. If you are not familiar with AS2 have a look at this basic Flash OOP -tutorial. However, even without that knowledge if you are familiar with AS you will understand. First we have the class declaration and here we use extend MovieClip and refer to the main movieclip in the fla file, which contains all the objects. Check here how to extend to another class. Before that we have to import some classes, since we have a textinput, button components and the Delegate class we need to extend the scope of some functions. Then we declare a number of variables we need including thisClip, which will refer to this, meaning the main movieclip on stage. Finally we have the constructor, where we initialize some functions. All is explained by the comments.

//
// importing classes
//
import mx.controls.TextInput;
import mx.controls.Button;
import mx.utils.Delegate;
class Memory extends MovieClip {
	//
	// set the position of the clips with the saved xml data
	//
	private var name:TextInput;
	private var messageField:TextField;
	private var square1:MovieClip;
	private var square2:MovieClip;
	private var square3:MovieClip;
	private var square4:MovieClip;
	private var setDataBut:Button;
	private var saveBut:Button;
	private static var thisClip:MovieClip;
	private var xset_name:XML;
	private var xstore_name:XML;
	//
	// The constructor
	//
	public function Memory () {
	   //
	   // restricting the entry of textinput to letters and numbers
	   //
	   this.name.restrict = "A-Z,a-z,1-9";
	   //
	   // setting focus to textinput
	   //
	   this.name.setFocus ();
	   //
	   // creating a variable to refer to "this"
	   //
	   thisClip = this;
	   //
	   // declaring the xml to load
	   // we need to cast "this.name.text" as xml, because it is a string. Then we will be able
	   // to load xml files with variable names.
	   //
	   xset_name = XML (this.name.text);
	   xset_name = new XML ();
	}

We now come to our first function, which is to set the stage from stored data. This is mainly parsing the xml file. If you are not very familiar how to do that in flash check this basic Flash - XML Tutorial or other tutorials. You may also want to use the Flash help files extensively, which give a lot of examples.

Basically what we do here is creating a button function for the Set Stage button using a listener. In order to extend the scope of the function we use the Delegate class. Then this within the function will refer to our movieclip. First we make sure that the user enters a valid name, which in our case has to have a minimum of 6 letters/numbers. if that is not the case we give a warning.

	//
	// we are setting the stage by loading the personal xml file.
	//
	public function setStage ():Void {
	   //
	   // listener for the button
	   //
	   var setData:Object = new Object ();
	   setData.click = Delegate.create (this, setDataFunction);
	   function setDataFunction () {
	       if (this.name.text == "" || this.name.text.length<6) {
	           this.messageField.text = "Enter name with at least 6 letters.";
	           return this.name.text;
	       }

The following lines show how to parse the xml file. All the square data are stored in individual nodes, which we access by using nextSibling. Now we can easily get the data by accessing the attributes of the individual nodes. Then we use the setProperty method to arrange the squares and we are done.

	       //  
	       // loading xml file and parsing
	       //
	       xset_name.onLoad = function (success:Boolean) {
	           if (success) {
	               //
	               // here we parse the xml to extract the data
	               //
	               var clipExt:XMLNode = this.firstChild.firstChild;
	               var myClip1:XMLNode = clipExt;
	               var myClip2:XMLNode = clipExt.nextSibling;
	               var myClip3:XMLNode = clipExt.nextSibling.nextSibling;
	               var myClip4:XMLNode = clipExt.nextSibling.nextSibling.nextSibling;
	               //
	               // setting the new values for the clips
	               //
	               setProperty (thisClip.square1, _x, myClip1.attributes.x_value);
	               setProperty (thisClip.square1, _y, myClip1.attributes.y_value);
	               setProperty (thisClip.square2, _x, myClip2.attributes.x_value);
	               setProperty (thisClip.square2, _y, myClip2.attributes.y_value);
	               setProperty (thisClip.square3, _x, myClip3.attributes.x_value);
	               setProperty (thisClip.square3, _y, myClip3.attributes.y_value);
	               setProperty (thisClip.square4, _x, myClip4.attributes.x_value);
	               setProperty (thisClip.square4, _y, myClip4.attributes.y_value);
	           } else {
	               //
	               // in case the file does not exist
	               //
	               thisClip.messageField.text = "The file does not exist!";
	           }
	       };
	       //
	       // this script will avoid caching the xml file
	       //
	       var thisFile:String = "folder/"+this.name.text+".xml";
	       if (System.capabilities.playerType != "External" && System.capabilities.playerType != "Standalone") {
	           thisFile += "?"+new Date ().getTime ()+Math.floor (Math.random ()*10000);
	       }
	       xset_name.load (thisFile);
	   }
	   this.setDataBut.addEventListener ("click", setData);
	}

On the next page we will look at the second function to send and store data.

NEXT PAGE            PREVIOUS PAGE

1      2      3      4