• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

MovieClips in an Array

New Here ,
May 01, 2009 May 01, 2009

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?

TOPICS
ActionScript

Views

543

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2009 May 01, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2009 May 01, 2009

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 01, 2009 May 01, 2009

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 01, 2009 May 01, 2009

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

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 01, 2009 May 01, 2009

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines