AS3 无法从自定义类访问阶段

发布于 2024-12-23 15:14:04 字数 472 浏览 6 评论 0原文

如何从自定义类访问舞台,尤其是 Flash 影片的宽度和鼠标位置?

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

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

这只会返回零。我知道 DisplayObjects 在启动之前没有任何阶段,因此您无法访问构造函数中的阶段,但即使我稍后调用 sayStage() 作为实例方法,它也无法工作。

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我一直都在从未离去 2024-12-30 15:14:04

如果 TableManager 在舞台上,您可以使用 this.stage 访问舞台。

诀窍是您必须等待实例添加到舞台中。您可以侦听 ADDED_TO_STAGE 事件,以便了解何时发生。

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

public class TableManager extends Sprite {
    public function TableManager() {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        sayStage();
    }

    public function sayStage():void {
        trace(this.stage);
    }
}   
}

If TableManager is on the stage you can access the stage with this.stage.

The trick is you have to wait for the instance to be added to the stage. You can listen for the ADDED_TO_STAGE event so you know when that's happened.

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

public class TableManager extends Sprite {
    public function TableManager() {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        sayStage();
    }

    public function sayStage():void {
        trace(this.stage);
    }
}   
}
混吃等死 2024-12-30 15:14:04

最防御性的写法是:

public function TableManager() {
    if(this.stage) init();
    else this.addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void {
    if(e != null) this.removeEventListener(Event.ADDED_TO_STAGE, init);
    sayStage();
}

如果对象在初始化时已经在舞台上了,那么立即调用不带参数的 init 函数。如果没有等到它被添加到舞台上。然后,当调用 init 函数时,如果它是作为事件结果调用的,则分离事件处理程序并继续前进。

The most defensive way to write this is:

public function TableManager() {
    if(this.stage) init();
    else this.addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void {
    if(e != null) this.removeEventListener(Event.ADDED_TO_STAGE, init);
    sayStage();
}

If the object is already on the stage at the time of initialization, then immediately call the init function with no arguments. If not wait until its been added to the stage. Then when the init function gets called, if it was called as the result of an event, then detach the event handler, and move along.

荒人说梦 2024-12-30 15:14:04

您可以将根影片剪辑(即舞台)的引用传递给您的自定义类。

例如

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

   public class TableManager extends Sprite
   {
       private var _rootMC:MovieClip; 

       public function TableManager(rootMC:MovieClip) {
            _rootMC = rootMC;
            sayStage();
        }
        public function sayStage():void 
        {
            trace(_rootMC.stage);
            trace(_rootMC.stage.stageWidth);
        }
  }   
} 

,当从根时间轴实例化 TableManager 实例时:

//the keyword 'this' is the root movieclip.
var newTM:TableManager = new TableManager(this);

You can pass a reference of the root movieclip (i.e. the stage) to your custom class.

e.g.

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

   public class TableManager extends Sprite
   {
       private var _rootMC:MovieClip; 

       public function TableManager(rootMC:MovieClip) {
            _rootMC = rootMC;
            sayStage();
        }
        public function sayStage():void 
        {
            trace(_rootMC.stage);
            trace(_rootMC.stage.stageWidth);
        }
  }   
} 

Then when instantiating your instance of TableManager from the root timeline:

//the keyword 'this' is the root movieclip.
var newTM:TableManager = new TableManager(this);
十级心震 2024-12-30 15:14:04

只要 Sprite 尚未添加到显示列表中,stage 将为 null - 这与启动无关。例如

var t:TableManager = new TableManager;
stage.addChild( t ); // or your root class, or any class that's already on the displaylist
trace( t.stage ); // [Stage stage]
t.parent.removeChild( t );
trace( t.stage ); // null

,正如@crooksy88建议的那样,要么将阶段传递给构造函数,要么将其作为静态保存在某处,例如您的主文档类,以便您可以在任何地方访问它。

stage will be null as long as the Sprite hasn't been added to the display list - it's nothing to do with initiation. E.g.

var t:TableManager = new TableManager;
stage.addChild( t ); // or your root class, or any class that's already on the displaylist
trace( t.stage ); // [Stage stage]
t.parent.removeChild( t );
trace( t.stage ); // null

As @crooksy88 suggests, either pass in the stage to the constructor, or keep it as a static somewhere, say your main document class, so that you can access it everywhere.

樱花细雨 2024-12-30 15:14:04

我认为对您有用的是创建对阶段的静态引用:

在您的主类中添加行并设置阶段:

public static var stage:Stage;
...
public function Main():void { // constructor
     Main.stage = stage;
     ...

而不是在自定义类中:

public function sayStage():void 
        {
            trace(Main.stage);
            trace(Main.stage.stageWidth);
        }

i think usefull for You should be create static reference to stage :

in Your main class add line and set stage :

public static var stage:Stage;
...
public function Main():void { // constructor
     Main.stage = stage;
     ...

and than in custom class :

public function sayStage():void 
        {
            trace(Main.stage);
            trace(Main.stage.stageWidth);
        }
眼泪淡了忧伤 2024-12-30 15:14:04

当当前对象(也是精灵)已经附加到舞台时,您可以访问 this.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

希望这可以帮助

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

π浅易 2024-12-30 15:14:04

这是一个非常好的解决方案,您只需要引用类中的阶段,您只需将其作为一个简单的对象传递,这里如何做到这一点,

package  {

    public class Eventhndl{

        private var obj:Object;

        public function Eventhndl(objStage:Object):void{
            obj = objStage;

            trace(obj); // now the obj variable is a reference to the stage and you can work as normal you do with stage (addChild, Events Etc..)

        }

}

这就是您创建实例来运行它的方式,我已经使用了构造函数方法,但您可以更改您可以根据需要将其添加到任何函数,并在需要时调用它。

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage);

这是一些如何从类访问阶段的示例

https://stackoverflow.com/a/40691908/1640362

https://stackoverflow.com/a/40691325/1640362

here is a pretty good solution you only need to reference the stage inside your class you just pass it as a simple object, here how to do that

package  {

    public class Eventhndl{

        private var obj:Object;

        public function Eventhndl(objStage:Object):void{
            obj = objStage;

            trace(obj); // now the obj variable is a reference to the stage and you can work as normal you do with stage (addChild, Events Etc..)

        }

}

this is how you make instance to run it, i have used the constructor method but you can change it to any function as you wish and call it whenever you need it.

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage);

here is some few Examples how to access stage from class

https://stackoverflow.com/a/40691908/1640362

https://stackoverflow.com/a/40691325/1640362

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文