【问题标题】:how do you associate a class file with a file in your library in AS3如何在 AS3 中将类文件与库中的文件相关联
【发布时间】:2013-01-03 08:21:53
【问题描述】:

我正在尝试开始使用名为 FlashDevelop 的 Flash IDE,它不使用我习惯的 .FLA 编辑器。我已经找到了如何将 /lib 文件夹中的文件嵌入到项目中,但我不确定如何像我之前所做的那样,将嵌入文件(例如 .jpg)与 .as 文件相关联以提供说明关于如何行动。下面是代表我尝试弄清楚的代码,在此先感谢您的帮助。

package 
{
    import flash.display.Bitmap;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;

    [Frame(factoryClass="Preloader")] //This references an auto generated preloader which I haven't touched
    public class Main extends Sprite 
    {
        [Embed(source="../lib/index.jpg")]
        public var logo:Class; //I'm attempting to connect the image in the above line to my logo.as file, all that's in there is an empty constructor and a simple update method that moves the image to the right
        public var pic:logo;

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

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            pic:logo = new logo();
            addChild(pic);
            addEventListener(Event.ENTER_FRAME, update);
        }

        private function update(e:Event):void 
        {
            pic.update();
        }



    }

}

【问题讨论】:

    标签: actionscript-3 embed


    【解决方案1】:

    您可以简单地将 index.jpg 嵌入和实例化移动到 logo.as。 (另外,如果你坚持用大写字母命名你的类,它可以帮助避免类和变量之间的混淆,所以例如我会称之为 Logo.as。)然后你的主类只需要添加new Logo()Logo.as 将负责创建实际图像。

    编辑:另外,请确保您不会偶然这样做:

    pic:Logo = new Logo();
    

    应该是这样的:

    pic = new Logo();
    

    【讨论】:

    • 这样做会导致错误消失,但图像不会在屏幕上呈现。我必须在 Logo.as 文件中做些什么来渲染图像吗?
    • 啊,您是在 Logo. 中添加图像作为子项,还是在 Main 中添加 new Logo() 作为子项?抱歉,我的回答不是很清楚。
    • 你能澄清一下吗?我应该只在 Logo.as 中,还是在 Main.as 和 Logo.as 中都有嵌入线?
    • 你只需要嵌入一次,在Logo.as中就更容易了。通过嵌入它,就像使用你给它的任何类名(例如private var imageClass:Class)向你的fla库添加一个图形,所以你需要创建一个new imageClass()并将它作为子元素添加到Logo,然后创建一个@ 987654327@ 并将其作为子项添加到 Main。
    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多