as3 - 从资产类获取库符号

发布于 2024-08-05 20:07:45 字数 551 浏览 2 评论 0原文

我创建了一个 asset.swf,我想在其中保留所有符号。然后,我创建了一个执行嵌入的 Assets 类。它看起来像这样:

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        public static var MyBox:Class;

        public function Assets() 
        {

        }

    }

现在,在其他一些类中,我想创建一个新框:

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = new Assets.MyBox();
    addChild(myBox);

}

我知道这是不正确的,并且我得到“TypeError:错误#1007:在非构造函数上尝试实例化”。如何访问 Assets 类中的资产?

I have created an assets.swf, in which I want to keep all my symbols. Then, I have created an Assets class which does the embedding. It looks like this:

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        public static var MyBox:Class;

        public function Assets() 
        {

        }

    }

Now, in some other class, I want to create a new box:

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = new Assets.MyBox();
    addChild(myBox);

}

I know this in incorrect, and I get "TypeError: Error #1007: Instantiation attempted on a non-constructor." How can I get access to the assets in the Assets class?

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

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

发布评论

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

评论(4

痴梦一场 2024-08-12 20:07:45

编辑:我想您会在此处找到答案。

以下内容适用于使用通过 Loader 类加载的 SWF 中的类。

private function onLoad(e:Event):void
{
 var domain:ApplicationDomain = LoaderInfo(e.target).applicationDomain;
 var Type:Class = domain.getDefinition("pack.MyComponent") as Class;
 var myBox:MovieClip = new Type();
 addChild(myBox);
}

Edit: I think you will find the answer here.

The following applies to using classes from an SWF loaded with Loader class.

private function onLoad(e:Event):void
{
 var domain:ApplicationDomain = LoaderInfo(e.target).applicationDomain;
 var Type:Class = domain.getDefinition("pack.MyComponent") as Class;
 var myBox:MovieClip = new Type();
 addChild(myBox);
}
月亮是我掰弯的 2024-08-12 20:07:45

实现它的另一个好方法是简单地编译 SWC 并像另一个类一样导入它。

SWC 内为 AS 导出的每个符号都将在相同范围内可供您使用。

这样可以节省大量代码编写工作,并将资源直接嵌入到 SWF 中,从而避免多个加载器。

Another good way to achieve it is simply compile the SWC and import it just like another Class.

Every symbol exported for AS inside your SWC will be available for you in the same scope.

This saves a lot code writing and embed the assets directly inside in your SWF, avoiding multiple loaders.

待"谢繁草 2024-08-12 20:07:45

如果您仍然需要 .swf 路径,您可以:

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        private static var _MyBox:Class;
        public static function get NewBox():MovieClip {
            return new _MyBox();
        }
    }
...

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = Assets.NewBox;
    addChild(myBox);

}

另一种方法是导出您想要由动作脚本使用的符号并使用 .swc。您可以在 flash 文件中设置具有完整命名空间的类,因此对于 MyBox,您可以将其设置为 com.company.Assets.MyBox,并且用法将与您的预期类似:

import com.company.Assets.*;
...
public function Game() 
{

    var myBox:MovieClip = new MyBox();
    addChild(myBox);

}

If you still want the .swf route, you can:

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        private static var _MyBox:Class;
        public static function get NewBox():MovieClip {
            return new _MyBox();
        }
    }
...

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = Assets.NewBox;
    addChild(myBox);

}

An alternative is to export the symbols you want to be used by Action Script and use the .swc instead. You can set the classes with a full namespace inside the flash file, so for MyBox you could make it com.company.Assets.MyBox and the usage would be similar to what you intended:

import com.company.Assets.*;
...
public function Game() 
{

    var myBox:MovieClip = new MyBox();
    addChild(myBox);

}
莫相离 2024-08-12 20:07:45

您可以在 Asset 类中创建新对象..(这还使您可以创建可重用资产池),

例如:

public class Assets extends MovieClip
{
    [Embed(source="assets.swf", symbol="MyBox")]
    private static var MyBox:Class;

    public static function getNewBox():DisplayObject {
        return new MyBox();
    }

    public function Assets() 
    {

    }
}

You can create new objects in the Asset class.. (that gives you also the possibility to make a pool of reusable assets)

something like:

public class Assets extends MovieClip
{
    [Embed(source="assets.swf", symbol="MyBox")]
    private static var MyBox:Class;

    public static function getNewBox():DisplayObject {
        return new MyBox();
    }

    public function Assets() 
    {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文