My Own Number

When teaching Flash one of the questions that often comes up is how to generate a menu based on XML. By the time the question gets to me usually the FLA is to the point where he XML is loaded, there is an imported Object from the library for each item in the XML and those Objects are clickable. Where it gets difficult is having each of those items remember what XML item they belong to.

In Actionscript 2.0 this was easy. We could assign whatever properties/variables/functions to whatever and everything would work out fine. Using Actionscript 3.0 we now hove to do this properly….well kind of.

First we need some XML to import. Lets use this very generic XML file.

<?xml version="1.0" encoding="utf-8"?>
<root_element>
     <item>
          <number>1</number>
          <title>Title Number 1</title>
          <image>image1.jpg</image>
     </item>
     <item>
          <number>2</number>
          <title>Title Number 2</title>
          <image>image2.jpg</image>
     </item>
     <item>
          <number>3</number>
          <title>Title Number 3</title>
          <image>image3.jpg</image>
     </item>
     <item>
          <number>4</number>
          <title>Title Number 4</title>
          <image>image4.jpg</image>
     </item>
</root_element>

Download the Example XML Document

Now lets go through two different methods of achieving this.

Method One: Avoiding External AS Files

The proper way to do this would be to create a Custom Class using an external Actionscript file and maybe partner this with a Library Object with the same name. However, this can be achieved without an External Actionscript file…it’s a little dirty but it’ll work.

Open up a new Actionscript 3.0 file and create a new Symbol with the following properties:

In this new Object give it the following timeline:

On the number text box put a Dynamic Text Field with the Instance name number_tb. On the Title layer put a Dynamic Text Field with the instance name title_tb. And on the background layer put some sort of background. IT should look something like this:

Now go back to the Stage and select Frame 1. The first thing we need to do is create a URL Request, a URL Loader and an XML Object:

var exampleXML:XML = new XML();
var exampleRequest:URLRequest=new URLRequest(&quot;flash_number_example.xml&quot;);
var exampleLoader:URLLoader = new URLLoader();

Next we need to load the URL Request using the URL Loader.

exampleLoader.load(exampleRequest);

When the loading is complete we will trigger the completeHandler Function.

exampleLoader.addEventListener(Event.COMPLETE,completeHandler);

And lastly we need to define that function.The first line takes the loaded data from our URL Loader and puts it into an XML object. Before we moved it into an XML Object the data would have been treated as text. This allows us to do all sorts of XML type things with it.

Next we loop through each item in the XML Object and create a copy of an ItemObject. For each ItemObject we set the test in the title_tb Text Field to the title from the XML and the number_tb Text Field to the item number. Next we position the Item Object, give it it’s Button actions and lastly add it to the Stage.

Special thanks to Mazoonist for the mouseChildren line of code. This solution was found in an actionscript.og forum post. This line makes sure the whole button is clickable. If we did not have this line the button would be clickable but you would not get the point mouse whenever your mouse is over a text box.

function completeHandler(evt:Event):void {
     exampleXML=XML(evt.target.data);
     for (var i:int=0; i<exampleXM.item.length(); i++ )
          var nextItem:ItemObject = new ItemObject();
          nextItem.title_tb.text = String(exampleXML.item[i].title);
          nextItem.number_tb.text = String(i);
          nextItem.x = 141 * i;
          nextItem.addEventListener(MouseEvent.CLICK,clickHandler);
          nextItem.mouseChildren = false;
          nextItem.buttonMode = true;
          addChild(nextItem);
     }
}

And of course we need a function to execute on he click of our Buttons. At this point this code just traces it’s own number. Now that each item know it’s number it can go back to the XML object and retrieve anything else it may need.

function clickHandler(evt:Event):void {
     trace(evt.target.number_tb.text);
}

Download the Hack Version FLA

Method Two: External AS Files

In this method we will use a Library Object in Partnership with an external AS file. Start by opening a new Actionscript 3.0 document. Just like the above example insert a new symbol with the following properties:

In that new symbol give it the following timeline:

On the title layer put a Text Field with the Instance name title_tb and on the background layer put some sort of background.

Next we need an external AS file to help define our ItemObject Object. Creating a Custom Class Actionscript file is exactly like creating an object in real life. We give the Object a name and we define what properties the Object has. In our case we are creating an ItemObject. From looking at our last method we know that an ItemObject is a type of MovieClip; which is actually a little over kill. We can go one step back. So our ItemObject is a type of Sprite.

The different between a Sprite and a MovieClip is that a MovieClip is a Sprite with a timeline. So MovieClip have properties like currentframe and all sorts of timeline related stuff.

When creating a Custom Class we use the package syntax and first list all pre-existing Classes the this Custom Class requires. So in our example we need the Sprite and MouseEvent Classes.

package {
     mport flash.display.Sprite;
     import flash.events.MouseEvent;

Next we start our Class and provide the base Class; in this case a Sprite:

     public class ItemObject extends Sprite {

We now need ot list what properties this new Custom Class has. In our example above the ItemObject has two properties that a Spite doesn’t have. We need a place to store the text and a pace to store the number.

          var itemText:String;
          var itemNumber:Number;

Next we need what is called a Constructor. A Constructor is the code that is executed when the Class is created. In the constructor definition we also specify what values are required to initially create an instance of our Custom Class.

In our example we need the text and the number to create an instance of an ItemObject. This function then saves the two provided values in the two properties defined above, adds a Rollover action and sets a few default properties.

          public function ItemObject(itemText:String,itemNumber:Number){
               this.itemText = itemText;
               this.itemNumber = itemNumber;
               this.title_tb.text = this.itemText;
               this.addEventListener(MouseEvent.CLICK,clickHandler);
               this.mouseChildren = false;
               this.buttonMode = true;
          }

Finally we need the clickHandler function called above. In this case we just trace the ItemObject’s number. And close the parenthesis from the open Class and Package lines above.

          public function clickHandler(evt:MouseEvent):void {
               trace(this.itemNumber);
          }

Download the Proper Version FLA

Tags: , ,

Leave a Reply