I've searched around and found similar questions to this, but the answers (accepted ones, even!) are either hacky, appear to have rather little to do with the actual question, or just aren't working when I try them.
I'm trying to reference objects on the stage in the current frame from a document class in Actionscript 3 in Flash CS3. In this example, I'm trying to get at a dynamic text field with the instance name "question_txt", but there are also buttons and other things I'll need to get at to put event listeners on and such.
I have "Automatically Declare Stage Instances" checked in the publish settings, so the references should be there -- in fact, if I try to declare them in the class, I get errors about a conflict with the name -- but when I try to reference these objects (in any of several ways I've now tried!) I ALWAYS GET NULL.
package {
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Stage;
import flash.text.TextField;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
public class DecisionTree extends MovieClip {
private var tree_xml:XML;
private var current_node:String;
public function DecisionTree():void {
trace("Constructor");
tree_xml = new XML();
tree_xml.ignoreWhitepace = true;
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE,
function(e:Event):void {
tree_xml = new XML(e.target.data);
goToNode('main');
});
loader.load(new URLRequest("decision_tree.xml"));
}
private function goToNode(name:String):void {
trace("goToNode " + name);
current_node = name;
node_xml = tree_xml.node.(attribute('name') == name)[0];
node_frame = parseInt(node_xml.attribute('frame'));
node_question = node_xml.question.text();
gotoAndPlay(node_frame); // will goto frame 35 where the stuff is
// ARRRRRGH EVERYTHING'S COMING UP NULL!!!
// TypeError: Error #1009: Cannot access a property or method of a null object reference.
question_txt.text = node_question;
//q = question_txt as TextField;
//q.text = node_question;
//qtxt = stage.getChildByName('question_txt') as TextField;
//qtxt.text = node_question;
}
}
}