
May 23rd, 2011, 05:09 PM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 27
Time spent in forums: 6 h 7 m 14 sec
Reputation Power: 0
|
|
I recently made a memory/concentration game where I randomly picked 8 pairs out of 20 cards and then randomly assigned them to places in the grid using this code:
Code:
//place cards in window
for (p=1; p<=numCards; p++){
var card:Card = new Card(); //new sprite
card.x=(((p%colCt)+1) * wid) - wid +18;
card.y=(trunc((p-1)/colCt)) * wid + 5;
this.addChild(card); //puts it on stage
}
Essentially, it loops through a list of cards (numCards=cardsChosen.length). Each run through creates a Card (custom class that inherits from MovieClip). I assign it a horizontal (x) position. wid is the width of the cards plus spacing (if your images are all the same size, you can assign wid explicitly somewhere, otherwise, you'll probably have to calculate the width you want). I added 18 to the width to offset from the edge of the stage, same for the 5 in the vertical (y) position. trunc is a custom function:
Code:
function trunc(val:Number):Number {
trace("function trunc");
var num = val.toString();
var float = num.indexOf(".");
if (float<0){
return val;
}
else{
return Number(num.substr(0,float));
}
}
If you don't want to create a custom class (like my Card class), you could put all the images in a movieClip (each in a different frame, with stop() in an added action layer that spans across the keyframes of the movieClip), and then before adding it to the stage, you can put card.gotoAndStop(chosenImg);
|