PDA

View Full Version : Referencing Text Field within Custom Class



Blazey
July 24th, 2008, 12:54 AM
I have set up two classes, one is the DocumentClass and it imports a custom class i have made called XMLLoader. The problem is I want to change the value of a dynamic text field on the stage with the instance name xml_txt from within the XMLLoader class...



package classes
{
import flash.display.MovieClip;
import classes.XMLLoader;

public class DocumentClass extends MovieClip
{
public function DocumentClass():void
{
var xml:XMLLoader = new XMLLoader();
}
}
}


package classes
{
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;

public class XMLLoader
{
var req:URLRequest = new URLRequest("myXML.xml");
var loader:URLLoader = new URLLoader();

public function XMLLoader():void
{
loader.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.addEventListener(Event.COMPLETE, loadComplete);
loader.load(req);
}

private function loadProgress(event:ProgressEvent):void
{
var percent:Number = ( 100 / event.bytesTotal ) * event.bytesLoaded;
trace(Math.round(percent));
}

private function loadComplete(event:Event):void
{
var newLoader:URLLoader = URLLoader(event.target);
trace(newLoader.data);
}
}
}

DiamondDog
July 24th, 2008, 03:34 PM
I don't know if my way's the most elegant way of doing this, but I'd create that dynamic text field at runtime, from within the Document Class, and then pass your instance of the Document Class into your XMLLoader constructor. You could then reference the dynamic text field no problem.

Attached are my three files - the .fla, the DocumentClass and the XMLLoader.

You'll see that I've not used the 'classes' name for the package, I've just got all three files in the same directory.

In my files, I also had to comment out the url reference as I didn't have the xml file you want to load.

But hey, I got the dynamic text field working. :bounce:

Hope that makes sense/helps.