5

In AS3 you can embed a graphic into a Class variable:

     [Embed(source="MenuAssets.swf", symbol="topSquare")]
        public var TopMenuItem:Class;

I have hundreds of assets in this one website project I'm doing, so I want to embed assets into an array for quick access.

Can I do something like this? Its not compiling so I'm wondering whether its possible.

        public var MenuAssets:Array = [
           [Embed(source="MenuAssets.swf", symbol="topSquare")],
           [Embed(source="MenuAssets.swf", symbol="botSquare")],
           [Embed(source="MenuAssets.swf", symbol="leftSquare")],
           [Embed(source="MenuAssets.swf", symbol="rightSquare")],
        ]

3 Answers 3

5

I'm afraid you can't. What you could do is the following:

public class Assets {
    [Embed(source="MenuAssets.swf", symbol="topSquare")]
    public static const TOP_SQUARE:Class;
    //... more assets ...
    public static function getAssets():Array {
        var ret:Array = [];
        for each (var s:String in describeType(Assets).constant.@name) ret.push(Assets[s]);
        return ret;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 That's cool! Maybe you can prefix all asset names with a unique string and filter them out from the constant.@name - just in case there are other constants declared in the same class.
@Amarghosh: good idea. OTOH, I think it would make sense to separate assets and actual constants into seperate classes.
I did this but ArgumentError: Error #2100: The ByteArray parameter in Loader.loadBytes() must have length greater than 0. at flash.display::Loader/_loadBytes() at flash.display::Loader/loadBytes()
@coderex I got this error when I load assets in iOS and was using TLF text. I needed to add the assets library to the swf that I loaded. I don't know if it was your case.
4

You could also embed the assets in a single FLA. In the FLA's library, give each one a class name like "graphics.menu.RightSquare" then export it as a SWC. Configure your Flash Builder project to load the SWC as an external library. Then you can do something like:

import graphics.menu.*;

new RightSquare();

1 Comment

Fantastic solution! Thanks a million, you've really saved me from a lot of manual [Embed] work.
4

In general, the metadata tags in Flex apply to a class level variable.

You must use the [Embed] metadata tag before a variable definition, where the variable is of type Class.

You can however do:

[Embed(source="MenuAssets.swf", symbol="topSquare")]
public var TopMenuItem:Class;

[Embed(source="MenuAssets.swf", symbol="leftSquare")]
public var LeftMenuItem:Class;

[Embed(source="MenuAssets.swf", symbol="rightSquare")]
public var RightMenuItem:Class;

[Embed(source="MenuAssets.swf", symbol="botSquare")]
public var BottomMenuItem:Class;

public var menuAssets:Array = [TopMenuItem, LeftMenuItem, 
                               RightMenuItem, BottomMenuItem];

1 Comment

+1. But you can generate the array automatically, as shown in my answer. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.