as3 - getting library symbols from an Assets class

 ̄綄美尐妖づ 提交于 2019-12-06 02:50:47

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);
}

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.

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);

}

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() 
    {

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!