views:

94

answers:

2

I'm trying to embed an x number of symbols with this:

[Embed(source='graphics/backgrounds.swf', symbol='background01')]
private var Background01 : Class;

the problem is that i have like 100 background symbols and would like to embed them without writing every single one like the code below here:

[Embed(source='graphics/backgrounds.swf', symbol='background01')]
private var Background01 : Class;

[Embed(source='graphics/backgrounds.swf', symbol='background02')]
private var Background02 : Class;

[Embed(source='graphics/backgrounds.swf', symbol='background03')]
private var Background03 : Class;

instead i would like to use 2 loops like this:

for (var i = 0;i < 10;i++)
{
    for (var j = 0;j < 10;j++)
    {
        [Embed(source='graphics/backgrounds.swf', symbol='background' + i + j )]
        // code for adding this to an 2d-array or something
    }
}

this wont work because it tells me that the meta data is wrong so my question is:

Is it possible to do this? or is there a similar way to do this?

+1  A: 

I don't believe you can put meta directives in regular code statements. They're part of the declaration of a variable in the scope of the class you're writing.

If writing so many variables proves onerous, and you have named the symbols sequentially as you have, write a javascript macro in your favorite text editor (I use and love ActiveState's KomodoEdit, and it's free) and have it spit out the text you can paste into your class ActionScript.

Robusto
+3  A: 

Yeah, sad to say Marcus, there's no way of doing what you're trying. MetaData is not AS3, it is not evaluated, so you can't use it the way that you are trying.

If however, you're going to embed the 100 background symbols anyway, why not create a single MovieClip with each one on a frame in the timeline? Then embed that symbol once and use gotoAndStop to go to the one you want?

100 backgrounds all loaded into memory at the start sounds like a dodgy way to go to me in general though. Better would probably be to load backgrounds in and out of memory as and when you need them. It depends what they are used for of course, but the name 'background' suggests to me that this might be a better course of action.

alecmce
how do i load them in and out of the memory then :p? is it possible to do this from an swf aswell?
Marcus
You would need to put each background in an individual swf, probably named background0.swf .. backgroundN.swf. Then you could load the swfs using a Loader. Place the Loader on the stage, then loader.load(new URLRequest("backround" + i + ".swf"));. When you want to swap backgrounds, add another loader behind it, listen to the Event.COMPLETE event using loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBackgroundSwap) then in onBackgroundSwap remove the previous background. Hope this helps. If you need more, I can add another answer in below to show you it more fully :)
alecmce
worked it out with the 100 backgrounds in a Movieclip thingy, my backgrounds are poor vectorgraphics so it did not take that much memory :)
Marcus