binlip
July 27th, 2005, 10:27 PM
I have searched many actionscript tutorials and forums in order to find a solution to my problem, so far no luck.
My problem:
To randomly deal 13 cards, face up, onto pre-designated coordinates of the main stage. Each card is, of course, different. Once dealt, how to get and set the value of each card to initiate the game?
Any help would be greatly appreciated.
binlip
Macaroni Ted
July 27th, 2005, 10:57 PM
this is off the top of my head but u could make one movieclip with all 52 card faces on indervidual frames. so giving u an instance with 52 frames. then find out all the coordinates you want to have the cards delt to and put them in an array after that use a for loop to attach 13 of your instance. eg:
cardX=newArray(//yourXcoordinates)
cardY=newArray(//yourYcoordinates)
for(i=1;i<cardX.length;i++){
var randNum=1+random(52);
card=attachMovie("playingCard","c"+i,i++);
card.gotoAndStop(randNum);
card._x=cardX[i];
card._y=cardY[i];
}
well something along those lines
binlip
July 28th, 2005, 02:22 PM
Thank you. I'll give it a try.
rmo518
July 28th, 2005, 06:36 PM
I think there may be a flaw in the approach above. Cards are not "removed" when they are randomly selected so you might end up with the same card showing up twice (which in a real deck would of course be impossible). Here's a remedy for that:
cardX=new Array(//yourXcoordinates)
cardY=new Array(//yourYcoordinates)
deckofcards = new Array(1,2,3,4 . . . 52);
function dealCard(){
cardPosition = Math.floor(Math.random() * deckofcards.length);
cardNum = deckofcards[cardPosition];
deckofcards.splice(cardPosition,1);
return cardNum;
}
for(i=1;i<=cardX.length;i++){
randNum = dealCard();
card=attachMovie("playingCard","c"+i,i++);
card.gotoAndStop(randNum);
card._x=cardX[i];
card._y=cardY[i];
}
Each time the function dealCard is invoked it selects a number at random from the deckofcards array and then splices it - removes it - so that the next time around it is only selecting from the pool of cards that haven't been chosen yet.
binlip
July 29th, 2005, 01:08 PM
Thanks for the input.
This is still above me.
Does the above script have to be repeated for each card position?
If you could, would you be able to recommend a good tutorial that would cover this type of function, ordered placement of a finite number of objects chosen randomly, and as you stated, each chosen object only placed once.
rmo518
July 31st, 2005, 11:24 PM
Okay, here's another revision of the code and a sample FLA for you to look at. You don't need to repeat the code for each card because the for loop runs through the thirteen coordinates in the cardX and cardY arrays. Those numbers give the playing card movie their x and y positions.
cardX=new Array(50,120,190,260,330,400,470,80,150,220,290,36 0,430);
cardY=new Array(110,110,110,110,110,110,110,200,200,200,200, 200,200);
deckofcards = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 ,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,3 5,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51, 52);
function dealCard(){
cardPosition = Math.floor(Math.random() * deckofcards.length);
cardNum = deckofcards[cardPosition];
deckofcards.splice(cardPosition,1);
return cardNum;
}
for(i=0;i<cardX.length;i++){
randNum = dealCard();
card=attachMovie("playingCard","c"+i,i);
card.gotoAndStop(randNum);
card._x=cardX;
card._y=cardY;
card.cardNumber = randNum;
}
In the FLA I made I have not taken the time to create 52 different cards in the playingCard movie, just a text field that displays the frame number so you can see what is going on. The last bit of code (card.cardNumber = randNum) does that, so you would probably remove it once you made the actual card graphics.
(Also, for some reason the deckofcards array is displaying strangely here, but it should just be the number 1 through 52 in sequence, separated by commas.)
I don't know of a specific tutorial that will help you with this, but one place you could start is by looking in the Flash ActionScript dictionary in the Actions panel under the entries that deal with the various bits of code used here. For example read about how arrays work, how the for loop works, and the attachMovie command. Those make up the bulk of this code.
binlip
August 1st, 2005, 05:06 PM
Hey look at that!
Thanks man.
Once I have the card graphics, how restricted am I in naming each card in order for the Math.random function to properly call up the cards?
Or will have to declare a numerical value for each of the 52 cards?
I'll have to invest more time in understanding actionscript.
virusescu
August 1st, 2005, 05:26 PM
Heve a look at this script too.. maybe it helps
http://www.kirupa.com/forum/showthread.php?t=185948 - It's the beggining of a draw poker card game. I predefined the cardPack.. and then use a the Math.random to randomly extract a card from the cardPack... of course I could have shuffled the cardpakc and then extract in order.. but I guess this seemed better at the moment.
See if it helps...
The zip file with the fla is in the seventh post in that thread
binlip
August 1st, 2005, 11:46 PM
Hello virusescu,
I loaded your poker game, no action (no pun intended). Version issue?
Heve a look at this script too.. maybe it helps
http://www.kirupa.com/forum/showthread.php?t=185948 - It's the beggining of a draw poker card game. I predefined the cardPack.. and then use a the Math.random to randomly extract a card from the cardPack... of course I could have shuffled the cardpakc and then extract in order.. but I guess this seemed better at the moment.
See if it helps...
The zip file with the fla is in the seventh post in that thread
virusescu
August 2nd, 2005, 03:01 AM
What do you mean by "no action"? It currently deals 5 cards form the cardPack and after the draw round it states what had you have. it's simple... not finished yet.
I was implying to take a look at the cardPack and the way the cards ar dealt on stage.
rmo518
August 2nd, 2005, 11:09 AM
Hey look at that!
Thanks man.
Once I have the card graphics, how restricted am I in naming each card in order for the Math.random function to properly call up the cards?
Or will have to declare a numerical value for each of the 52 cards?
I'll have to invest more time in understanding actionscript.
I don't know if this is what you're asking about or not, but the random function is just pulling a frame number from 1 to 52. You should be able to put whatever graphics you want in each of the 52 frames of the card movie without having to name or rename anything.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.