As3从时间轴引用一个类

时间:2011-12-10 07:13:34

标签: android flash actionscript-3 adobe

我无法弄清楚发生了什么事?我可以将其他类导入时间轴并使用它们就好了,但是这个类给了我很大的问题?我正在从我的服务器上解析XML数据,它给我的错误看起来像这样。

时间表参考和用法:

     import networkScores;

     var network:networkScores = new networkScores();
     addChild(network);

     score1Textfield.text = network.score1.toString();

班级定义:

 package 
 {
          import flash.net.URLLoader;
          import flash.net.URLRequest;
          import flash.events.Event;
          import flash.display.MovieClip;

         public class networkScores extends MovieClip
         {
                 public var myXML:XML, myXMLNames:XML;
                 public var xmlLoaderScores = new URLLoader();
                 public var score1:int;

                 public function networkScores()
                 {
                     xmlLoaderScores.addEventListener(
                                Event.COMPLETE, xmlLoadedScores);
                     xmlLoaderScores.load(new URLRequest("pathtoxmlfile"));
                 }

                 public function xmlLoadedScores(e:Event):void
                 {
                      myXML = new XML(e.target.data);
                      var qName1:QName = new QName(
                            "http://www.w3.org/2005/Atom", "score1");
                      score1 = myXML.descendants(qName1)[0].toString();
                 }

        }

     }

我得到的错误:

  

场景1,图层'动作',第4帧,第149行1119:通过带有静态类型networkScores的引用访问可能未定义的属性score1。

     

1067:将类型为networkScores的值隐式强制转换为不相关的类型flash.display:DisplayObject。

这是一个投射问题吗?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的时间表不应该尝试引用文档类。文档类应该控制一切。有几种方法可以知道何时将对象添加到舞台并准备好进行寻址(例如侦听添加到舞台)。一旦您知道该对象已添加并且知道您的xml已返回,您就可以在文档类中公开的子对象上填充变量。有关详情,请查看此博文http://www.developria.com/2010/04/combining-the-timeline-with-oo.html以及此处附带的代码http://flexdiary.blogspot.com/2010/04/sample-code-for-oop-timeline-insideria.html

已编辑以显示代码:

它的代码可能如下所示:

package {
   public class Main extends MovieClip {
      private var _stageInstance:StageInstance;
      private var _score:String;
      private var _loader:URLLoader;//hold loader in memory so it doesn't gc before it returns
      //by using a getter/setter pair, we know when Flash has added the instance to the stage
      public function get stageInstance():StageInstance{
          return _stageInstance;
      }
      public function set stageInstance(value:StageInstance):void {
         _stageInstance = value;
         if (_stageInstance != null && _score != null) {
             _stageInstance.score = _score;
         }
      }
      public function onScoreLoaded(e:Event):void {
         myXML = new XML(e.target.data);
         var qName1:QName = new QName("http://www.w3.org/2005/Atom", "score1");
         _score = myXML.descendants(qName1)[0].toString();
         if (stageInstance != null) {
            stageInstance.score = _score;
         }
      }
      public function Main() {
         _loader = new URLLoader();
         _loader.addEventListener(Event.COMPLETE, onScoreLoaded);
         _loader.load(new URLRequest('pathToXML'));
      }
   }

}

package {
   public Class StageInstance extends MovieClip {
       pubic var score1TextField:TextField;//populated by Flash Player
       private vare _score:String;
       //note how the setter here is doing something useful, not just passing through the value
       public function get score():String {
          return _score;
       }
       public function set score(value:String):void {
          _score=value;
          score1TextField.text=score;
       }
       public function StageInstance() {
          super();
       }

   }

}