To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 09-28-2004, 05:43 PM   #1
Evelyne1976
Junior Member
 
Join Date: Apr 2004
Posts: 10
shuffle card game

Hi all! I am posting an .fla of what I am trying to do, perhaps someone can help me figure it out.

I am creating a card game, and at the end of the game if the player wants to replay the game, I want my

cards to shuffle. What I have managed so far in this demo file, is when the game starts the movie clips

are placed in certain positions on the stage, and when you press the button instead of shuffeling...all

movie clips are placed behind the other and they all move along. What I want instead is on each mouse

press, the cards to swap places and only one movie clip per place! Appreciate anyone's wisdom!

P.s. I HAVE flash Mx...NOT...flash MX 2004.
Attached Files
File Type: zip shufflemovieclips.zip (5.5 KB, 1271 views)
Evelyne1976 is offline   Reply With Quote
Old 09-28-2004, 06:03 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Here's a modified version of your code that will do it. I was able to shorten your code a bit by using the same function for shuffling the cards to perform the initial placement.

code:

// Some Useful constants - better to encode these up here
// than to have them as literals inside your code...
kLeftMargin = 80;
kTopMargin = 80;
kCellWidth = 70;
kCellHeight = 70;
kNbrColumns = 4;
kNbrRows = 4;
kTotalCards = kNbrColumns*kNbrRows;

// The basic trick:
// You can convert a single number n to a pair of card indexes (x,y)
// using x = (n % kNbrColumns) and y = int(x/kNbrColumns)
// I use this technique a couple of times below.

// Function to place cards in random order
ShuffleAndDeal = function()
{
// create an array of card indexes
shuffleOrder = [];
for (var i = 0; i < kTotalCards; ++i) {
shuffleOrder[i] = i;
}
// shuffle the array using a random sort
shuffleOrder.sort(function(){ return random(2)? -1 : 1});
trace("Order: " + shuffleOrder);

// use the array to select cards
for (var i = 0; i < kTotalCards; ++i)
{
r = shuffleOrder[i];

// card index
// note: if your cards were numbered consecutively (0,1,2) then you
// could simply use 'r' and don't need to compute rx,ry
rx = r % kNbrColumns;
ry = int(r/kNbrColumns);

// position on board
ix = i % kNbrColumns;
iy = int(i/kNbrColumns);
piece = _root[ry+"-"+rx];
piece._x = kLeftMargin + iy*kCellWidth;
piece._y = kTopMargin + ix*kCellHeight;
}
}

stop();
ShuffleAndDeal(); // initial placement

press_mc.onRelease = function() {
ShuffleAndDeal();
}




- Jim Bumgardner

EDIT: I've modified the code a few times as I thought of optimizations.
__________________

Last edited by jbum; 09-28-2004 at 06:11 PM.
jbum is offline   Reply With Quote
Old 09-28-2004, 06:24 PM   #3
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
You may enjoy the following card trick, which I made in Flash a few weeks ago.

Mind Power

Instructions.

A wheel of cards will appear. Mentally choose one of the cards, and also choose a 4 letter word of your choosing.

Let's say you choose the 7 of hearts, and the word "BARN".

Using the handle, rotate the wheel so that your card is under the first letter of your word. So you'd rotate it so the seven of hearts is under the B.

Then press the red-button in the middle.

At this point, there's no way to know either your card, or the word chosen, since each card is under a different letter.

Then rotate the wheel so the seven is under the "A" in "BARN", and press the button again.

Repeat for the remaining two letters.

You'll be astonished at what happens next.

- Jim
__________________
jbum is offline   Reply With Quote
Old 07-17-2005, 01:05 AM   #4
bibibear84
Jolene
 
Join Date: Jul 2005
Posts: 1
You rocks!

Jbum,

your code for shuffling an array rocks!!! I have been working it for so long, yet cannot find an answer... but i modified your codes and place it into my application and it works!!! Cool !!!
bibibear84 is offline   Reply With Quote
Old 03-11-2008, 06:42 PM   #5
pjfarr
Junior Member
 
Join Date: Mar 2008
Posts: 5
Great code, Jim.

Almost bang-on what I was looking for. My cards are 2 frame movieclips. Frame 1 is the "backing", Frame 2 is the "face". They start out face down and when clicked on, the cards switch to Frame 2 (showing their value).

With your code above, it rearranges the order of the cards (which is great) but it doesn't flip the ones showing Frame 2 back to Frame 1.

Is there a way to do it?
pjfarr is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:30 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.