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

后端 未结 5 532
余生分开走
余生分开走 2021-01-28 05:56

I am working on a 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

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 06:41

    I think some dependency injection will solve this problem. When you instantiate your Typewriter class, pass references to myTA and myTI. ex:

    public function Main{ 
        testTypeWriter(this.myTA, this.myTI); 
    } 
    

    Then your Typewriter constructor should look like this:

    public function TypeWriter(ta:TextArea, ti:TextArea){ 
        this.myTA = ta;
        this.myTI = ti;
      } 
    

    This also has the benefit of making your application less tightly coupled, so for example you can reuse your Typewriter class with a different text area and text input.

    Edit

    Some extra info that may help you in the future: you can access stage elements through the root object. But this only works with objects that have been added to the display list. Let's say that Typewriter represents an object in the display list, you could then access myTA like this:

    MovieClip(root).myTA
    

    (Change MovieClip to Sprite if that's what your document class extends).

    However, since it seems that Typewriter does not get added to the display list, I recommend using my first suggestion of dependancy injection.

    Also check out this page, it dicusses using CasaLib to access the stage from any object. I personally haven't tried it, so that's why it's at the end here ;-)

提交回复
热议问题