如何使非文档级类“知道”在Flash AS3阶段组件? [英] how do I make non-document-class classes 'aware' of stage components in Flash AS3?

查看:158
本文介绍了如何使非文档级类“知道”在Flash AS3阶段组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个文字冒险游戏,将至少有几个组成部分(文本区域叙述和文字输入的用户输入)在舞台上在​​任何时候。因此,我有静态通过Flash的所见即所得的设计环境中创建这些组件。我给他们实例名称myTA」及「myTI。我能得到我的主类(为舞台文档类),与他们进行互动(在像打字机在运行一段时间动态地添加文字的一个字符),而其他类在同一个包似乎并没有能够认识舞台上的组件。下面是相关的code:

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 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:

方案A中,一切都在主类中发生的:

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);           
      }  
    } 
}

案例B,在第二类打字机,其中主要调用来处理打字机打印的:

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

主营:

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, I am a test...");  
        }             
        addChild(typeW); 
      } 

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

打字机:

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);           
      }  
    } 
}

方案A的工作,但如果B闪光是给我的错误错误#1009:无法访问空对象引用的属性或方法,并注意到在打字机的第一行,我试试myTA作为操作问题。

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?

谢谢

CCJ

推荐答案

我会建议 Service Locator模式< /一>这一点。最朴素的方法是创建一个包含公共静态变量的资源类。然后在你的文档类分配的阶段,实例在资源类对应的静态变量。然后,你可以简单地在任何地方访问这些阶段的组成部分。

I would recommend the Service Locator Pattern for this. The most naive approach would be to create a resource class which contains public static variables. Then in your document class you assign the stage instances to the corresponding static variable in the resource class. Then you can simply access these stage components anywhere.

var someTextArea = Resource.TA; //probably should rename to something more meaningful

有关的东西多一点巧妙的,你应该阅读我联系的文章。

For something a little more ingenious you should read the article I linked to.

我觉得这比依赖注入的构造器注入可能会导致巨大的参数列表,你可以添加更多的项目到舞台更好,我不是那么喜欢的setter注入,因为它很容易忘记对其进行设置。

I think this is better than the dependency injection as constructor injection could lead to huge parameter list as you might add more items to the stage, and I am not so fond on setter injection as it is easy to forget to set them.

编辑:

只是为了使它更有点清楚,我想我会增加一些code:)

Just to make it a bit more clear I thought I would add some code :)

资源类

package
{
    //TODO imports
    public class Resource
    {
        public static var TA:TextArea;
        public static var TI:TextInput;
    }
}

文档类

//....setup function
Resource.TA = myTA; //myTA is the name of the instance on stage
Resource.TI = myTI;

Foo类

Resource.TA.x = 100;
//or
_myClassMemberVariable = Resource.TA;
_myClassMemberVariable.x = 100;

这篇关于如何使非文档级类“知道”在Flash AS3阶段组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆