AS3全球一流的可添加对象阶段对象、阶段、全球

由网友(指尖的执念)分享简介:所以,我最近才知道,通过导入类到我的主类,我可以从其它类访问它的函数。但....我在导入的类函数需要显示对象添加到舞台。我访问的静态函数得很好,但它不能在舞台上添加对象。它甚至不似乎承认的addChild。这是因为它不是显示列表本身?在So I recently learnt that by importing a...

所以,我最近才知道,通过导入类到我的主类,我可以从其它类访问它的函数。但....我在导入的类函数需要显示对象添加到舞台。我访问的静态函数得很好,但它不能在舞台上添加对象。它甚至不似乎承认的addChild。这是因为它不是显示列表本身?在

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是这样的:

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是在我的库中的项目。

BreakPop being an item in my library.

在此先感谢。

推荐答案

由于您使用的是静态方法,你的 PopHandler 不是一个精灵的一个实例,因此没有访问属性。如果你想离开它作为一个静态方法,那么你基本上可以跳过扩展Sprite ,只是去了公共类PopHandler {

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 {

这出的方式,这里有一个简单的解决问题的方法:为什么不通过的DisplayObjectContainer 你想添加的 BreakPop 来作为你的静态方法的参数?

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?

例如:

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
阅读全文

相关推荐

最新文章