Aug 13 2008
Clarification for “First Class Ever”
It was brought to my attention that some people would like to access not only the features of the stage (addChild(), eventListeners, etc) but also the DisplayObjects that reside there. If you played around with the files provided in the original post and tried to do something like this:
private function checkText(evt:Event):void {
if (_stage.myInput.text == "test text") {
_stage.addChild(customText);
dispatchEvent( new Event(correctText) );
}
}
you would quickly learn that the AS3 compiler can get really annoying. Obviously this should work, right? I mean, its an explicit reference to the stage! Well, true, it is an explicit reference to the stage, but that isn’t what you need. What you need is an explicit reference to “root”. Now here is where I am going to vague because I don’t fully understand what the difference is, nor why its necessary, but I can tell you from exploring the children of ’stage’ that “root” seems to be a display object one level below “stage” where the real children (the ones we care about at least) reside.
That said, we will be passing “root” to the class in the same way we passed “stage” except that I don’t actually know what data type “root” is (called “MainTimeline” by Flash’s trace() function). My quick fix is to use *. Here is the updated code for the class:
package com.kylebrekke {
import flash.display.Sprite;
// Our class extends the Sprite base class
public class testClass extends Sprite {
// Imported files for the compiler
import fl.controls.TextInput;
import flash.text.TextField;
import flash.events.Event;
import flash.display.Stage;
// Public Event Constants
public static const correctText:String = "correctText";
// Private variables
private var stageTextfield:TextInput; //check using event change
private var customText:TextField = new TextField();
private var _stage:Stage;
private var _root:*;
// The 'getters' and the 'setters'
public function get customizedText():String {
return customText.text;
}
public function set customizedText(s:String):void {
customText.text = s;
}
// This is the constructor, the first function to fire during instantiation
public function testClass(alreadyOnStage:TextInput, s:Stage, r:*):void {
// We are passing a reference of the Input Text into a class variable
// so that we can access it from anywhere inside our class
stageTextfield = alreadyOnStage;
stageTextfield.addEventListener(Event.CHANGE, checkText);
_stage = s;
_root = r;
// This custom text can be overriden by the getters/setters
customText.text = "Correct Text!";
customText.x = 250;
customText.y = 35;
}
// This function processes the input text if its value ever changes
private function checkText(evt:Event):void {
if (_root.myInput.text == "test text") {
_stage.addChild(customText);
dispatchEvent( new Event(correctText) );
}
}
}
}
and here is the one line you need to change in the .fla:
var test:testClass = new testClass(myInput, stage, root);
I hope that clarifies everything. Please post any comments, especially if you happen to know a bunch about how the stage works in AS3.
Also, for another guide on this, try this link.
Popularity: unranked [?]