I want to call stage.stagewidth/stage.stagehieght from imported class. here is my code hope any one can fix it or tell me how to do it

package 
 {
  import flash.display.MovieClip;
  import multyLoader;
  public class program extends MovieClip
    {
     private var theLoader:multyLoader = new multyLoader();
     public function program()
       {
      // constructor code-----------------
      theLoader.My_Loader(mc_loaderHolder,myXML.IMAGE[1].@URL);
       }
     }
 }

and the multyLoader.as file code is

 package 
 {
  public class multyLoader extends MovieClip
   {
    public function multyLoader()
     {
      trace(any);
     }
    public function My_Loader(loading_holder,myLoaderURL:String)
     {
      // pla pla code
      loading_holder.x = (stage.stageWidth - loading_holder.width) / 2; // get Error #1009 or stage not found.
      loading_holder.y = (stage.stageHeight - loading_holder.height) / 2; // get Error
     }      
   }
 }
有帮助吗?

解决方案

If you want to reference the Stage from your multyLoader instance, then it needs to be on the display list first. The safest way to do that is to listen for the ADDED_TO_STAGE event and only reference the stage after that event has fired.

public function My_Loader(loading_holder, myLoaderURL:String){
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

protected function onAddedToStage(e:Event){
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    loading_holder.x = (stage.stageWidth - loading_holder.width) / 2;
    // etc...
}

You could also just make sure to only call the My_Loader function after you are sure theLoader has been added to the display list. It's not always as straightforward as it seems though.

A third option, if you don't want theLoader to be on the display list for whatever reason, would be to pass theLoader a reference to the stage from the parent class. You could add it as an argument to your My_Loader function.

public function My_Loader(loading_holder, myLoaderURL:String, myStage:Stage){
    loading_holder.x = (myStage.stageWidth - loading_holder.width) / 2;
    // etc...
}

calling it from the parent class like this:

theLoader.My_Loader(mc_loaderHolder, myXML.IMAGE[1].@URL, this.stage);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top