PDA

View Full Version : [AS3] making classes 'aware' of stage components


CCJ
11-29-2010, 08:53 PM
Hello all,

I am working on text adventure game which will have at least a few components (a text area for narrative and text input for user input) on the stage at all times. Therefore, I have created those components statically through Flash's WYSIWYG design environment. I gave them instance names "myTA" and "myTI" respectively. I was able to get my main class (the document class for the stage) to interact with them (dynamically adding text one character at a time like a typewriter at runtime), but other classes in the same package don't seem able to recognize the stage components. Below is the relevant code:

Case A, in which everything happens within the Main class:

package {
public class Main extends MovieClip {
public var myTA:TextArea;
var displayedChar:String = new String();
var textToWrite:String = new String();
var i:int = 0;
var intervalId:uint;
var done:int = 0;

public function Main {
setUpTA();
}

public function setUpTA(){
myTA.text = "" + playAtInterval("Hello Player!");
}

public function writeCharsSlowly(){
textToWrite = arguments[0];
displayedChar = textToWrite.substring(i,i+1);
myTA.appendText(displayedChar);


i++;
if (i == textToWrite.length) {
done = 1;
clearInterval(intervalId);
}

}


public function playAtInterval(theText:String) {
i = 0;
intervalId = setInterval(writeCharsSlowly, 100, theText);

}
}
}


Case B, where Main calls on a second class 'TypeWriter' to handle the typewriter-printing:
Main:

package {
public class Main extends MovieClip {
public var myTA:TextArea;
public var myTI:TextInput;
var str:String = new String();

public function Main{
testTypeWriter();
}

public function testTypeWriter(){
typeW.playAtInterval("Hello Player"); typeW.addEventListener(MouseEvent.CLICK,testTypeWr iter2); typeW.addEventListener(KeyboardEvent.KEY_DOWN,inpu tEngine2)
addChild(typeW);
}

public function testTypeWriter2(event:MouseEvent){
if (myTI.text == "a") {
typeW.playAtInterval("yo");
}
else{
typeW.playAtInterval("Greetings, I am a test...");
}

addChild(typeW);
}

public function inputEngine2(event:KeyboardEvent){
str = String.fromCharCode(event.charCode);
myTI.appendText(str);
}


TypeWriter:

package {

public class TypeWriter extends MovieClip {
public var myTI:TextInput;
public var myTA:TextArea;
var i:int = 0;
var done:int = 0;
var intervalId:uint;
var displayedChar:String = new String();
var textToWrite:String = new String();

public function TypeWriter(){
///nothing here
}

public function writeCharsSlowly(){
textToWrite = arguments[0];
displayedChar = textToWrite.substring(i,i+1);
myTA.appendText(displayedChar);


i++;
if (i == textToWrite.length) {
done = 1;
clearInterval(intervalId);
}

}


public function playAtInterval(theText:String) {
i = 0;
intervalId = setInterval(writeCharsSlowly, 100, theText);

}
}
}



Case A works, but in case B Flash is giving me the error "Error #1009: Cannot access a property or method of a null object reference" and notes the first line in TypeWriter where I try to operate on myTA as the problem.

how can I make other classes besides the document class 'aware' of existing stage components?

Thanks,

CCJ

henke37
11-29-2010, 09:17 PM
Please read my first article and it should be much clearer.

CCJ
11-30-2010, 04:24 PM
I read the first article and it cleared up some questions I had about the timeline, but I'm still confused as to why my non-document-class classes don't recognize the components on the stage... any additional advice would be very much appreciated!