user2771951
user2771951

Reputation: 11

ActionScript 3.0 Document class getting stage as null

My Document class is not extending to movie clip class. It is getting stage reference as null. Please help me to get me out of this. Thanks in advance.

Code for Document Class:

package 
{
    import com.andreanaya.areacontrol.Container;
    import paperwings.display.area.Build;
    import paperwings.display.components.generic.TopMenu;
    import paperwings.webservices.analytics.Tracker;

    public class PaperWings extends Container 
    {
        private var _build : Build;
        private var _menu : TopMenu;
        public function PaperWings():void 
        {
            super();
            init();
        }


        override public function init() : void 
        {
            SWFWheel.initialize(stage);
            this._build = new Build();
            this._menu = new TopMenu();
            addChild(this._menu);
            this._menu.show(0.5);

            addChild(this._build);
            this._build.init();
            super.init();

        } 

    }

}

Code for Inherited class : This is class from which main class is inherited..

    package com.andreanaya.areacontrol
{
   import flash.display.Sprite;
   import flash.display.Stage;
   import flash.display.DisplayObject;
   import flash.system.ApplicationDomain;


   public class Container extends Sprite
   {

      public function Container() {
         super();
         trace("construcor.. Container.")
         this._paths = new Array();
         this._containers = new Array();
         this._content = new Array();
         addEventListener(AreaControlEvent.SHOW,this.onChange);
         addEventListener(AreaControlEvent.HIDE,this.onChange);
         addEventListener(AreaControlEvent.UPDATE,this.onChange);
      }

      private var _paths:Array;

      private var _containers:Array;

      private var _content:Array;

      private var _container:Container;

      private var _className:String;

      private var _status:String = "hide";

      private var _changes:int;

      private var _pathNames:Array;

      private var _param:Object;

      override public function get stage() : Stage {
         return AreaControl.stage;
      }

      public function init() : void {
          trace("container init ");
         dispatchEvent(new AreaControlEvent(AreaControlEvent.INIT));
      }

      public function show(param1:Array=null, param2:Object=null) : void {
         dispatchEvent(new AreaControlEvent(AreaControlEvent.SHOW));
      }

      public function hide() : void {
         dispatchEvent(new AreaControlEvent(AreaControlEvent.HIDE));
      }

      public function update(param1:Array=null, param2:Object=null) : void {
         dispatchEvent(new AreaControlEvent(AreaControlEvent.UPDATE));
      }

      public function preload(param1:Array=null, param2:Object=null) : void {
         dispatchEvent(new AreaControlEvent(AreaControlEvent.PRELOAD));
      }

      override public function addChild(param1:DisplayObject) : DisplayObject {
         var _loc2_:uint = 0;
         if(param1  is  Container)
         {
            _loc2_ = this._containers.length;
            while(--_loc2_ > -1)
            {
               if(this._containers[_loc2_] == param1)
               {
                  return super.addChild(param1);
               }
            }
            param1.addEventListener(AreaControlEvent.CHANGE,this.onChange);
            this._containers.push(param1);
         }
         return super.addChild(param1);
      }

      override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject {
         var _loc3_:uint = 0;
         if(param1  is  Container)
         {
            _loc3_ = this._containers.length;
            while(--_loc3_ > -1)
            {
               if(this._containers[_loc3_] == param1)
               {
                  return super.addChildAt(param1,param2);
               }
            }
            param1.addEventListener(AreaControlEvent.CHANGE,this.onChange);
            this._containers.push(param1);
         }
         return super.addChildAt(param1,param2);
      }

      override public function removeChild(param1:DisplayObject) : DisplayObject {
         var _loc2_:uint = 0;
         if(param1  is  Container)
         {
            _loc2_ = this._containers.length;
            while(--_loc2_ > -1)
            {
               if(this._containers[_loc2_] == param1)
               {
                  this._containers.splice(_loc2_,1);
                  return super.removeChild(param1);
               }
            }
            this._containers.push(param1);
         }
         return super.removeChild(param1);
      }

      override public function removeChildAt(param1:int) : DisplayObject {
         var _loc3_:uint = 0;
         var _loc2_:DisplayObject = super.removeChildAt(param1);
         if(_loc2_  is  Container)
         {
            _loc3_ = this._containers.length;
            while(--_loc3_ > -1)
            {
               if(this._containers[_loc3_] == _loc2_)
               {
                  this._containers.splice(_loc3_,1);
                  return _loc2_;
               }
            }
         }
         return _loc2_;
      }

      public function addPath(param1:String, param2:String=null) : void {
         var _loc3_:uint = this._paths.length;
         while(--_loc3_ > -1)
         {
            if(this._paths[_loc3_].pathName == param1)
            {
               this._paths[_loc3_].className = param2;
               return;
            }
         }
         this._paths.push(
            {
               "pathName":param1,
               "className":param2
            }
         );
      }

      public function removePath(param1:String) : void {
         var _loc2_:uint = this._paths.length;
         while(--_loc2_ > -1)
         {
            if(this._paths[_loc2_].pathName == param1)
            {
               this._paths.splice(_loc2_,1);
               return;
            }
         }
      }

      private function checkPath(param1:String) : Object {
         var _loc2_:uint = this._paths.length;
         while(--_loc2_ > -1)
         {
            if(this._paths[_loc2_].pathName == "*" || this._paths[_loc2_].pathName == param1)
            {
               return this._paths[_loc2_];
            }
         }
         return null;
      }

      private function createContent(param1:String) : Container {
         var _loc2_:uint = this._content.length;
         while(--_loc2_ > -1)
         {
            if(this._content[_loc2_].className == param1)
            {
               this._content[_loc2_].container.addEventListener(AreaControlEvent.CHANGE,this.onChange);
               this._content[_loc2_].container.change(this._pathNames,this._param);
               super.addChildAt(this._content[_loc2_].container,0);
               return this._content[_loc2_].container;
            }
         }
         var _loc3_:Class = ApplicationDomain.currentDomain.getDefinition(param1) as Class;
         var _loc4_:Container = new _loc3_();
         _loc4_.addEventListener(AreaControlEvent.INIT,this.onInit);
         _loc4_.init();
         this._content.push(
            {
               "className":param1,
               "container":_loc4_
            }
         );
         return _loc4_;
      }

      function check(param1:Array=null) : Boolean {
         var _loc2_:uint = 0;
         if(!param1 || this._paths.length == 0)
         {
            return false;
         }
         if(param1.length > 0)
         {
            _loc2_ = this._paths.length;
            while(--_loc2_ > -1)
            {
               if(this._paths[_loc2_].pathName == "*")
               {
                  return false;
               }
               if(this._paths[_loc2_].pathName == param1[0])
               {
                  return true;
               }
            }
         }
         if((this._container) && (this._container.check(param1)))
         {
            return true;
         }
         var _loc3_:Array = param1.concat();
         _loc3_.splice(0,1);
         _loc2_ = this._containers.length;
         while(--_loc2_ > -1)
         {
            if(this._containers[_loc2_].check(_loc3_))
            {
               return true;
            }
         }
         return false;
      }

      function change(param1:Array=null, param2:Object=null) : void {
         if(!param1 || this._paths.length == 0)
         {
            dispatchEvent(new AreaControlEvent(AreaControlEvent.CHANGE));
            return;
         }
         this._pathNames = param1;
         this._param = param2;
         var _loc3_:Object = param1.length > 0?this.checkPath(param1[0]):null;
         this._changes = 0;
         if(_loc3_)
         {
            if(this._status == "show")
            {
               this._status = "update";
            }
            else
            {
               this._status = "show";
            }
            this._changes++;
            addEventListener(AreaControlEvent.PRELOAD,this.onPreload);
            this.preload(param1,param2);
         }
         else
         {
            if(this._status == "show")
            {
               this._changes++;
               this._status = "hide";
               if(this._container)
               {
                  this._changes++;
                  this._container.change([]);
               }
               this.changeChildren([]);
               if(this._changes == 1)
               {
                  this.hide();
               }
            }
            else
            {
               dispatchEvent(new AreaControlEvent(AreaControlEvent.CHANGE));
               return;
            }
         }
      }

      private function onPreload(param1:AreaControlEvent) : void {
         var _loc2_:Object = this._pathNames.length > 0?this.checkPath(this._pathNames[0]):null;
         if(_loc2_.className)
         {
            if(this._container)
            {
               if(this._className == _loc2_.className)
               {
                  this._changes++;
                  this._container.change(this._pathNames,this._param);
               }
               else
               {
                  this._changes++;
                  this._container.addEventListener(AreaControlEvent.CHANGE,this.removeContent);
                  this._container.change(this._pathNames,this._param);
                  this._changes++;
                  this._container = this.createContent(_loc2_.className);
               }
            }
            else
            {
               this._changes++;
               this._container = this.createContent(_loc2_.className);
            }
         }
         else
         {
            if(this._container)
            {
               this._changes++;
               this._container.change(this._pathNames,this._param);
            }
         }
         this._className = _loc2_.className;
         var _loc3_:Array = this._pathNames.concat();
         _loc3_.splice(0,1);
         this.changeChildren(_loc3_,this._param);
         if(this._status == "show")
         {
            this.show(this._pathNames,this._param);
         }
         else
         {
            if(this._status == "update")
            {
               this._status = "show";
               this.update(this._pathNames,this._param);
            }
         }
      }

      private function changeChildren(param1:Array=null, param2:Object=null) : void {
         var _loc3_:uint = this._containers.length;
         while(--_loc3_ > -1)
         {
            this._changes++;
            this._containers[_loc3_].change(param1,param2);
         }
      }

      private function onInit(param1:AreaControlEvent) : void {
         this._container = param1.currentTarget as Container;
         super.addChildAt(this._container,0);
         this._container.removeEventListener(AreaControlEvent.INIT,this.onInit);
         this._container.addPath(this._pathNames[0]);
         this._container.addEventListener(AreaControlEvent.CHANGE,this.onChange);
         this._container.change(this._pathNames,this._param);
      }

      private function removeContent(param1:AreaControlEvent) : void {
         param1.currentTarget.addEventListener(AreaControlEvent.CHANGE,this.onChange);
         param1.currentTarget.removeEventLisntener(AreaControlEvent.CHANGE,this.removeContent);
         super.removeChild(param1.currentTarget as DisplayObject);
      }

      private function onChange(param1:AreaControlEvent) : void {
         trace("on change"+this._changes)
         this._changes--;
         if(this._changes == 0)
         {
            this.dispatchEvent(new AreaControlEvent(AreaControlEvent.CHANGE));
         }
      }
   }

}

Upvotes: 0

Views: 175

Answers (1)

KumoKairo
KumoKairo

Reputation: 678

There's a common sense of accessing your stage after the Event.ADDED_TO_STAGE event fired. Like this (I'll just edit your existing code)

public class PaperWings extends Container 
{
    private var _build : Build;
    private var _menu : TopMenu;
    public function PaperWings():void 
    {
        super();
        //PROBLEM HERE
        //If you call init with your stage == null,
        //it will throw an error when you are initializing your SWFWheel lib
        init();
    }

    override public function init() : void 
    {
        //ERROR HERE
        SWFWheel.initialize(stage);
        //=========================
        this._build = new Build();
        this._menu = new TopMenu();
        addChild(this._menu);
        this._menu.show(0.5);

        addChild(this._build);
        this._build.init();
        super.init();

    } 
....

So you just edit it as I've shown you - it will most likely solve your problem

public class PaperWings extends Container 
{
    private var _build : Build;
    private var _menu : TopMenu;
    public function PaperWings():void 
    {
        super();
        //We check our stage reference first, so we won't get any error
        if(stage)
            init();
        else
            addEventListener(Event.ADDED_TO_STAGE, init);
    }

    override public function init(e:Event = null) : void 
    {
        //stage is never null here.
        SWFWheel.initialize(stage);
        this._build = new Build();
        this._menu = new TopMenu();
        addChild(this._menu);
        this._menu.show(0.5);

        addChild(this._build);
        this._build.init();
        super.init();

    } 

And btw, I'd suggest you using this lib to block browser scroll instead. It's more lightweight and well optimized ;)

Upvotes: 0

Related Questions