从.as文件添加到ActionScript 3中的舞台 [英] Adding to Stage in ActionScript 3 from a .as file

查看:104
本文介绍了从.as文件添加到ActionScript 3中的舞台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:是的,我知道以前也曾问过类似的问题。但是,在遵循了此类问题的答案之后,我仍然卡住,无法找到解决问题的方法。

Note: Yes, I know that similar questions have been asked before. However, after following the answers in such questions I'm still stuck and can't find a solution to my problem.

我遇到了需要添加DisplayObjects的问题进入Flash阶段。由于必须显示几个不同类的元素,因此我决定创建一个类来充当.as文件和 addChild 函数 Displayer之间的中介,如图所示

I'm having a problem which requires adding DisplayObjects to the Flash stage. Since I have to Display elements of several different classes, I decided to create a class to work as an intermediary between the .as files and the addChild function called "Displayer" as shown below:

package
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;

    public class Displayer extends Sprite //I read somewhere that DisplayObject
//as an extension can't be used for this, so Sprite will have to do.
    {
        private var _stage:Stage;

        function Displayer()
        {
            _stage = new Stage;
        }

        public function displayElement(displayable:DisplayObject)
        {
            _stage.addChild(displayable);
        }
    }
}

我编译它并出现我不明白的问题:错误#2012:无法实例化Stage类。显然,此代码中的某些内容丢失或不适当,但是由于它非常简单,所以我看不到问题出在哪里。我确信它不是很复杂,我可能只需要一个局外人的视角。

I compile it and there appears a problem that I don't understand: Error #2012: Can't instantiate Stage class. Evidently, something in this code is either missing or out of place, but since it's so straightforward I fail to see what the problem can be. I'm sure that it's not very complicated, I probably just need an outsider's perspective.

推荐答案

Stage对象不能全局访问。您需要通过DisplayObject实例的stage属性访问它。

The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance.

请参考以下文档。

< a href = http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Stage.html rel = nofollow> http://help.adobe.com/zh_CN/ FlashPlatform / reference / actionscript / 3 / flash / display / Stage.html

package
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;

    public class Displayer extends Sprite
    {
        var isAddedToStage:Boolean;

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

        private function init(e:Event=null):void
        {
           removeEventListener(Event.ADDED_TO_STAGE, init);
           isAddedToStage = true;
        }

        public function displayElement(displayable:DisplayObject):void
        {
            if(isAddedToStage)
              this.stage.addChild(displayable);
        }
    }
}

这篇关于从.as文件添加到ActionScript 3中的舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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