Skip to main content
JDBird1000
Known Participant
July 22, 2009
Answered

Learning about arrays

  • July 22, 2009
  • 2 replies
  • 817 views

I found this code that shows me how to put my MC in a array and add one to the stage but in what way would i need to change it to add more to the stage  say nine from a random 100 mcs...

var card1:card001 = new card001();
var card2:card002 = new card002();
var card3:card003 = new card003();
var card4:card004 = new card004();
var card5:card005 = new card005();
var card6:card006 = new card006();
var card7:card007 = new card007();
var card8:card008 = new card008();
var card9:card009 = new card009();

var allImages:Array = new Array();
allImages = [card1, card2, card3, card4, card5, card6, card7];


var newImage = allImages[Math.floor(Math.random() * allImages.length)]

addChild(newImage);
trace (newImage)
newImage.x = 8;
newImage.y = 5;

This topic has been closed for replies.
Correct answer Ned Murphy

I was going to remove my offering and direct you to MaxManNH, but not for the reasons you cited... those reasons are new information that can lead to a different response.

2 replies

Inspiring
July 22, 2009

Create an array with a for loop that contains string names ["card001", "card002", "card003"........] etc.

Then randomly pull out 9 of the strings and use the getDefinitionByName function to get the movieclip from the library.

var nc:Class = getDefinitionByName(allImages[randomIndex]) as Class;

var obj = new nc() as DisplayObject;

addChild(obj);

It is a little more involved since you will need to code the loops and such, but getDefinitionByName is where you want to start.

JDBird1000
Known Participant
July 22, 2009

Thanks that’s a start

Ned Murphy
Legend
July 22, 2009

for(var i:uint=0; i<9; i++){

     var newImage = allImages[Math.floor(Math.random() * allImages.length)]

     addChild(newImage);
     trace (newImage)
     newImage.x = 8;
     newImage.y = 5;

}

You probably would want to adjust the position

JDBird1000
Known Participant
July 22, 2009

Thanks... had it working by copy and pasting var and arrays and renameing every thing in sequence... but im getting tierd of doing things that way

Ned Murphy
Ned MurphyCorrect answer
Legend
July 22, 2009

I was going to remove my offering and direct you to MaxManNH, but not for the reasons you cited... those reasons are new information that can lead to a different response.