actionscript 3 - how do I make non-document-class classes 'aware' of stage components in Flash AS3? -


i working on text adventure game have @ least few components (a text area narrative , text input user input) on stage @ times. therefore, have created components statically through flash's wysiwyg design environment. gave them instance names "myta" , "myti" respectively. able main class (the document class stage) interact them (dynamically adding text 1 character @ time typewriter @ runtime), other classes in same package don't seem able recognize stage components. below relevant code:

case a, in happens within 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) {                         = 0;                       intervalid = setinterval(writecharsslowly, 100, thetext);                  }       }  } 

case b, main calls on second class 'typewriter' handle 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,testtypewriter2);         typew.addeventlistener(keyboardevent.key_down,inputengine2)          addchild(typew);        }         public function testtypewriter2(event:mouseevent){          if (myti.text == "a") {              typew.playatinterval("yo");           } else {              typew.playatinterval("greetings, 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) {                         = 0;                       intervalid = setinterval(writecharsslowly, 100, thetext);                  }       }  } 

case works, in case b flash giving me error "error #1009: cannot access property or method of null object reference" , notes first line in typewriter try operate on myta problem.

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

thanks,

ccj

i recommend service locator pattern this. naive approach create resource class contains public static variables. in document class assign stage instances corresponding static variable in resource class. can access these stage components anywhere.

var sometextarea = resource.ta; //probably should rename more meaningful 

for little more ingenious should read article linked to.

i think better dependency injection constructor injection lead huge parameter list might add more items stage, , not fond on setter injection easy forget set them.

edit:

just make bit more clear thought add code :)

resource class

package {     //todo imports     public class resource     {         public static var ta:textarea;         public static var ti:textinput;     } } 

document class

//....setup function resource.ta = myta; //myta name of instance on stage resource.ti = myti; 

foo class

resource.ta.x = 100; //or _myclassmembervariable = resource.ta; _myclassmembervariable.x = 100; 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -