0 Replies Latest reply on Oct 16, 2007 8:16 AM by Newsgroup_User

    AS3: static vars and on-stage MC's

    Level 7
      hi,
      my basic problem is as follows:
      i have one swf which loads another one. both of them have document class.
      i want to access a variable of the main swf's class from the inner swf's
      class, so i turned that var into static. it works fine, as long as i don't
      access MC's on the main swf's stage from its class. i've tried to solve it
      using the 'ApplicationDomain' but it still fails.
      for testing purposes i've made 2 swf's - 'main.swf' and 'inner.swf', where
      on the main's stage i've placed a button, with instance name 'myBtn'.

      this is the main's class:
      package {
      import flash.display.MovieClip;
      import flash.display.Loader;
      import flash.net.URLRequest;
      import flash.system.*;
      import flash.events.*;
      public class MainClass extends MovieClip {
      private static var myStatic:String = "abc";
      private var ldLoader:Loader;
      public function MainClass(){
      loaderInfo.addEventListener(Event.INIT, myOnInit);
      }
      private function myOnInit(eEvent){
      ldLoader = new Loader();
      var urlRequest = new URLRequest("inner.swf");
      var context:LoaderContext = new LoaderContext (false,
      ApplicationDomain.currentDomain);
      ldLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
      completeHandler);
      ldLoader.load(urlRequest, context);
      this.myBtn.addEventListener(MouseEvent.MOUSE_UP,btnUp);
      }
      private function btnUp(evt){
      trace("btn up");
      }
      private function completeHandler(evt){
      var inner = this.addChild(evt.target.content);
      inner.myOnInit();
      }
      static function get staticVar():String{
      return myStatic;
      }
      }
      }
      -----------------------------
      this is the inner's class:
      package {
      import flash.display.MovieClip;
      public class InnerClass extends MovieClip{
      public function InnerClass(){}
      public function myOnInit(){
      trace(MainClass.staticVar);
      }
      }
      }

      when i compile 'inner' i get the following error:
      1119: Access of possibly undefined property myBtn through a reference with
      static type MainClass.
      the only way to avoid this error is to comment the last line of the
      'myOnInit' function in the main class, however then there's no access to the
      main's MC's.

      any solution?
      thanks in advance,
      eRez
      www.allofme.com