Copy link to clipboard
Copied
Is it possible to load up an array with movieclip instances from the library and use that as a reference to go to a certain index, snag that mc, and display it on the stage?
Copy link to clipboard
Copied
Pretty much... try it and see if it does what you want. I've have used arrays in such a manner, but not with the same usage of the movieclips in mind. If it doesn't work as planned, show the code you're using and clarify the intent of it.
Copy link to clipboard
Copied
Yes, you could do that. As an example, if you had a different library movieclip for every one of the cards in a pack of playing cards, you could do this (assuming you've shared the movieclips in the Library with these names):
var packArray:Array = [];
packArray.push(new AceOfHearts());
packArray.push(new TwoOfHearts());
packArray.push(new ThreeOfHearts());
packArray.push(new FourOfHearts());
and so on for 52 pushes! Then you could add a random card to the stage:
addChild(packArray[Math.floor(Math.random()*packArray.length)]);
This isn't to say that it's the best way to work!
Copy link to clipboard
Copied
Thanks Colin, that was enough to give me the clue what I was going wrong. I wasn't loading my array up with "new" instances. I was trying to create a new instance upon retrieval and was getting errors.
Your way gets me where I wanted to go - and the real funny thing is that your example of cards is exactly what I'm trying to do, although my deck has 90 cards! Plus, I'm using splice to remove the card from the mix once it's been dealt. So thanks to you I'm back on track again!
Copy link to clipboard
Copied
I used cards as an example because it was hard to think of a case where this might be the right way to work!
Incidentally, you should take a look at getDefinitionByName() too. The syntax is somewhat strange, but it's on these lines:
var ClassReference:Class = getDefinitionByName("someClassName") as Class; var instance:Object = new ClassReference();
You could get the Class of say ("Card"+i) if your movieclips were shared as Card1 - Card90. Making the whole array routine be something like:
var i:int;
var ClassRef:Class;
var cards:Array = []
for(i=1;i<=90;i++){
ClassRef = getDefinitionByName("Card"+i) as Class;
cards.push(new ClassRef());
}
Copy link to clipboard
Copied
Wow, that was really slick! Really tightened things up. I was thinking of something along those lines but the wacky way of trying to concatenate the instance name and communicate with the array was not working at all.
Thanks a bunch!