可以将对象添加到舞台的 AS3 全局类

发布于 2024-12-29 19:35:10 字数 809 浏览 2 评论 0原文

所以我最近了解到,通过将一个类导入到我的主类中,我可以从任何其他类访问它的函数。但是......我在导入类中的函数之一需要将显示对象添加到舞台。我访问静态函数很好,但它无法将对象添加到舞台。它甚至似乎无法识别 addChild。这是因为它不在显示列表本身中吗?

我在这里缺少什么?大家会怎么解决这个问题呢。我是如此接近,却又如此遥远!

代码如下:

package {

import flash.display.Sprite;
import PopHandler;

public class MyMainClass extends Sprite {
    public function MyMainClass():void {
        PopHandler.LaunchPop();
    }
}

}

这是导入的类,不会向舞台添加任何内容。

package {
import flash.display.Sprite;

public class PopHandler extends Sprite {

    public function PopHandler():void {

    }

    public static function LaunchPop() {
        var bp:BreakPop = new BreakPop();
        bp.x = 500;
        bp.y = 347;
        addChild(bp);
    }
}   

BreakPop

是我图书馆中的一个项目。

提前致谢。

So I recently learnt that by importing a class into my main class I can access its functions from any other class. BUT.... one of my functions in the imported class needs to add display objects to the stage. I accessed the static function just fine but it can't add objects to the stage. It doesn't even seem to recognise addChild. Is this because it is not in the display list itself?

What am I missing here? How would you guys solve this issue. I was so close, yet so far!

Code is like this:

package {

import flash.display.Sprite;
import PopHandler;

public class MyMainClass extends Sprite {
    public function MyMainClass():void {
        PopHandler.LaunchPop();
    }
}

}

This is the imported class that doesn't add anything to stage.

package {
import flash.display.Sprite;

public class PopHandler extends Sprite {

    public function PopHandler():void {

    }

    public static function LaunchPop() {
        var bp:BreakPop = new BreakPop();
        bp.x = 500;
        bp.y = 347;
        addChild(bp);
    }
}   

}

BreakPop being an item in my library.

Thanks in advance.

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

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

发布评论

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

评论(2

浮云落日 2025-01-05 19:35:10

扩展您的主文档类并添加 static var ,然后在 init 上添加 stage :

public class Main extends MovieClip {

   public static var stage:Stage;

   public function Main ():void {
     Main.stage = stage;
   }
}

然后在应用程序中的任何位置您都可以访问 stage :

Main.stage.addChild(element)

extend You main document class and add there static var , than assing stage on init :

public class Main extends MovieClip {

   public static var stage:Stage;

   public function Main ():void {
     Main.stage = stage;
   }
}

and then anywhere in app You can access to stage :

Main.stage.addChild(element)
雨落星ぅ辰 2025-01-05 19:35:10

由于您使用的是静态方法,因此您的 PopHandler 不是精灵的实例,因此无法访问 stage 属性。如果您想将其保留为静态方法,那么您基本上可以跳过 extends Sprite 并直接使用 public class PopHandler {

这是一个简单的方法问题的解决方案:为什么不传递您想要添加 BreakPopDisplayObjectContainer 作为静态方法的参数?

示例:

public static function LaunchPop(target:DisplayObjectContainer) {
    var bp:BreakPop = new BreakPop();
    bp.x = 500;
    bp.y = 347;
    target.addChild(bp);
}

然后你这样称呼它(一些示例):

PopHandler.LaunchPop(this); // <-- adding to current object
PopHandler.LaunchPop(root as DisplayObjectContainer); // <-- adding to root
PopHandler.LaunchPop(stage); // <-- adding to stage

Since you're using a static method, your PopHandler isn't an instance of a sprite and therefore doesn't have access to the stage property. If you want to leave it as a static method, then you can basically skip the extends Sprite and just go for public class PopHandler {

That out of the way, here's a simple solution to your problem: Why not pass the DisplayObjectContainer you'd like to add the BreakPop to as a parameter of your static method?

Example:

public static function LaunchPop(target:DisplayObjectContainer) {
    var bp:BreakPop = new BreakPop();
    bp.x = 500;
    bp.y = 347;
    target.addChild(bp);
}

Then you call it like this (some examples):

PopHandler.LaunchPop(this); // <-- adding to current object
PopHandler.LaunchPop(root as DisplayObjectContainer); // <-- adding to root
PopHandler.LaunchPop(stage); // <-- adding to stage
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文