AS3 无法从自定义类访问阶段
如何从自定义类访问舞台,尤其是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果 TableManager 在舞台上,您可以使用
this.stage
访问舞台。诀窍是您必须等待实例添加到舞台中。您可以侦听
ADDED_TO_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.最防御性的写法是:
如果对象在初始化时已经在舞台上了,那么立即调用不带参数的
init
函数。如果没有等到它被添加到舞台上。然后,当调用 init 函数时,如果它是作为事件结果调用的,则分离事件处理程序并继续前进。The most defensive way to write this is:
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 theinit
function gets called, if it was called as the result of an event, then detach the event handler, and move along.您可以将根影片剪辑(即舞台)的引用传递给您的自定义类。
例如
,当从根时间轴实例化 TableManager 实例时:
You can pass a reference of the root movieclip (i.e. the stage) to your custom class.
e.g.
Then when instantiating your instance of TableManager from the root timeline:
只要
Sprite
尚未添加到显示列表中,stage
将为 null - 这与启动无关。例如,正如@crooksy88建议的那样,要么将阶段传递给构造函数,要么将其作为静态保存在某处,例如您的主文档类,以便您可以在任何地方访问它。
stage
will be null as long as theSprite
hasn't been added to the display list - it's nothing to do with initiation. E.g.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.
我认为对您有用的是创建对阶段的静态引用:
在您的主类中添加行并设置阶段:
而不是在自定义类中:
i think usefull for You should be create static reference to stage :
in Your main class add line and set stage :
and than in custom class :
当当前对象(也是精灵)已经附加到舞台时,您可以访问 this.stage 。
希望这可以帮助
you may access this.stage when the current object(also a sprite) is already attached to the stage.
hope this could help
这是一个非常好的解决方案,您只需要引用类中的阶段,您只需将其作为一个简单的对象传递,这里如何做到这一点,
这就是您创建实例来运行它的方式,我已经使用了构造函数方法,但您可以更改您可以根据需要将其添加到任何函数,并在需要时调用它。
这是一些如何从类访问阶段的示例
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
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.
here is some few Examples how to access stage from class
https://stackoverflow.com/a/40691908/1640362
https://stackoverflow.com/a/40691325/1640362