views:

722

answers:

4

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?

+1  A: 

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);
}
Amarghosh
A: 

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.

goo
A: 

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

}
eglasius
A: 

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

    }
}
kajyr