2
votes

I cannot figure out what is going on? I can import other classes to the timeline and use them just fine, but this class is giving me major problems? I am parsing XML data from my server on it and it is giving me errors that look like this.

The timeline reference and usage:

     import networkScores;

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

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

The class definition:

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

     }

The errors I am getting:

Scene 1, Layer 'Actions', Frame 4, Line 149 1119: Access of possibly undefined property score1 through a reference with static type networkScores.

1067: Implicit coercion of a value of type networkScores to an unrelated type flash.display:DisplayObject.

Is this a casting issue?

how can I fix this?

1
could you tell me which line got which error? - apscience
Im sorry, yes, the addChild(network); line received the 1067 error, and the network.score1.toString(); line got the 1119 error. - AgnosticDev
Your code looks fine, I think flash can't find the class networkScores's definition. Where is the networkScores.as file? - Louis
the network scores file is in the same directory as my .xfl file - AgnosticDev
@Scientific xfl file? Only fla and mxml file I have used. Is it the same as the fla? - Louis

1 Answers

1
votes

Your timeline should not be trying to refer to the document Class. The document Class should control everything. There are several ways to know when an object has been added to the stage and is ready to be addressed (such as listening for added to stage). Once you know the object has been added and you also know your xml has returned, you can then populate a variable on the child object that you've exposed in its document Class. For more on this, check out this blog post http://www.developria.com/2010/04/combining-the-timeline-with-oo.html , and the accompanying code here http://flexdiary.blogspot.com/2010/04/sample-code-for-oop-timeline-insideria.html .

EDITED to show code:

The code for it might look something like this:

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

   }

}