AS3 Cannot access stage from custom class

2019-07-20 04:08发布

How can I access the stage and especially the width and mouse position of the flash Movie from a custom class?

package classes
{
   import flash.events.*;
   import flash.display.*;

   public class TableManager extends Sprite
   {
        public function TableManager() {
            sayStage();
        }
        public function sayStage():void 
        {
            trace(stage);
        }
  }   
} 

This will only return nill. I know that DisplayObjects don't have any stage until they have been initiated so you can't access the stage in your constructor but even if I call sayStage() later as an instance method it won't work.

What am I doing wrong?

7条回答
太酷不给撩
2楼-- · 2019-07-20 04:45

you may access this.stage when the current object(also a sprite) is already attached to the stage.

public class TableManager extends Sprite{
    public function TableManager()
    {
    }
    public function sayStage():void 
    {
        trace(stage);
    }
}
TableManager tm=new TableManager();
//tm.sayStage();  // no
addChild(tm);  
tm.sayStage();  // fine

hope this could help

查看更多
登录 后发表回答