AS3全球一流的可添加对象阶段 [英] AS3 Global class that can add objects to stage

查看:216
本文介绍了AS3全球一流的可添加对象阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我最近才知道,通过导入类到我的主类,我可以从其它类访问它的函数。但....我在导入的类函数需要显示对象添加到舞台。我访问的静态函数得很好,但它不能在舞台上添加对象。它甚至不似乎承认的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

这篇关于AS3全球一流的可添加对象阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆