I’ve written some code that creates four buttons and assigns their labels from an xml file. Unfortunately, the way it works there’s hardly any point of bothering with xml because I just have to write out each TweenButton instance name, its initial parameters, listeners, inclusion on the display list, etc, all within the createTheButtons function anyway. All in all it seems a very bloated and ineleagant way of doing it and I would like to make it more dynamic and scaleable.
For starters, how can I condense everything that’s presently in the createTheButtons function into a for loop, have it increment through __buttonLabelsXML, and use its index to dynamicaly name each Tween button instance, set its initial parameters and listeners and add it to the display list?
I know it’s a lot to ask, but could someone please show me?
Here’s my code and source files:
package
{
import flash.display.*;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.xml.XMLNode;
import TweenButton;
import flash.events.*;
public class Base extends MovieClip {
private var __myButton1:TweenButton;
private var __myButton2:TweenButton;
private var __myButton3:TweenButton;
private var __myButton4:TweenButton;
private var __buttonLabelsXML:XMLList;
private var __buttonUrls:XMLList;
public function Base() {
loadTheXML("xmlFile.xml");
addEventListener(Event.CHANGE, createTheButtons);
}
public function loadTheXML(theXML:String) {
var externalXML:XML;
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(theXML);
loader.load(request);
loader.addEventListener(Event.COMPLETE, onComplete);
}
public function onComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
externalXML = new XML(loader.data);
var buttonLabelsXML:XMLList = new XMLList(externalXML.children().attribute("label"));
var buttonUrlsXML:XMLList = new XMLList(externalXML.children().attribute("url"));
__buttonLabelsXML = buttonLabelsXML;
__buttonUrlsXML = buttonUrlsXML;
dispatchEvent(new Event(Event.CHANGE));
}
public function createTheButtons(e:Event):void {
var myButton1:TweenButton = new TweenButton(150, 150, __buttonLabelsXML[0], __buttonUrlsXML[0]);
__myButton1 = myButton1;
var myButton2:TweenButton = new TweenButton(150, 185, __buttonLabelsXML[1], __buttonUrlsXML[1]);
__myButton2 = myButton2;
var myButton3:TweenButton = new TweenButton(150, 220, __buttonLabelsXML[2], __buttonUrlsXML[2]);
__myButton3 = myButton3;
var myButton4:TweenButton = new TweenButton(150, 255, __buttonLabelsXML[3], __buttonUrlsXML[3]);
__myButton4 = myButton4;
myButton1.addEventListener(Event.CHANGE, bringToFront);
myButton2.addEventListener(Event.CHANGE, bringToFront);
myButton3.addEventListener(Event.CHANGE, bringToFront);
myButton4.addEventListener(Event.CHANGE, bringToFront);
addChild(myButton1);
addChild(myButton2);
addChild(myButton3);
addChild(myButton4);
}
public function bringToFront(e:Event):void {
var num:Number = this.numChildren;
var i:Number = getChildIndex(e.currentTarget);
swapChildrenAt(i,num-1);
}
}
}
package
{
import flash.display.*;
import flash.events.*
import flash.text.TextField;
public class TweenButton extends MovieClip {
private var __over:Boolean;
public var msg:TextField;
private var __url:String;
public function TweenButton(posX:Number, posY:Number, label:String, url:String) {
__url = new String();
__url = url; // __the url and clickHandlers aren't set up yet, not worried about that at the moment.
__over = new Boolean();
__over = undefined;
this.msg.text = label;
this.x = posX;
this.y = posY;
this.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
}
public function overHandler(e:MouseEvent):void {
__over = true;
this.addEventListener(Event.ENTER_FRAME,playForwards);
this.dispatchEvent(new Event(Event.CHANGE));
}
public function outHandler(e:MouseEvent):void {
__over = false;
this.addEventListener(Event.ENTER_FRAME,playBackwards);
}
public function playForwards(e:Event):void {
if(__over == true){
this.nextFrame();
}
}
public function playBackwards(e:Event):void {
if(__over == false){
this.prevFrame();
}
}
}
}
Fingers