
Simply put how do I extract a number from an Array so that it is not used again in a random loop? here is a example.
placeholder = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
randomPlaceholder = placeholder.length;
i = "0";
while (Number(i)<randomPlaceholder) {
a = (20 * Math.random(randomPlaceholder));
a = (Math.round(a));
trace ("The number for placeholder randomly selected is"+a);
a = n
i = Number(i)+1;
**************************************
*** randomPlaceholder.splice(n,1); *
**************************************
Now here I want to extract this number from the array so that it is not used again. I tried this and I have tried a few different ways but I can't get it to work. Help!******
}

Ok... here's a function you can use:
function getRandomArrayItem (arrayName) {
var n = random(eval(arrayName).length);
outVar = eval(arrayName)[n];
eval(arrayName).splice(n, 1);
return (outVar);
}
To use it, you simply call the function with a string containing the name of the array you want to use.
In context, you could use it like this:
var nextNumber = getRandomArrayItem("placeholder");
Note that your array had to be declared globaly or this won't work. Hope this helps.
Josh

I see where you are going but I am not needing to call it, simply cycle through scattering the cards to different locations when it hits this frame. the problem I am having is that with the script the way it is, it is creating duplicate numbers. This is the reason why I need to remove the number generated from the array. So you can better understand what is happening, here is the whole thing.
Basically what is happening is when the MC hits this frame it randomly selects where each card goes. (note this is for a card matching game). Now although I realize this is not finished by anymeans, I just want to make sure that the place holder is only selecting one of each number in the array. (*don't worry about the card number array it's the same issue, and im solving one at a time*)
Thanks for your help!
// Generate Random Locations for cards
placeholder = new Array(20);
cardnumber = new Array(20);
n = "";
a = "";
l = "";
k = "";
i = "0";
PlaceHolderLength = placeholder.length;
CardNumberLength = cardnumber.length;
while (Number(i)<PlaceHolderLength) {
randomPlaceholder = PlaceHolderLength
randomCardNumber = CardNumberLength
a = (20 * Math.random(randomPlaceholder));
a = (Math.round(a));
trace ("The number for placeholder randomly selected is"+a);
k = (20*Math.random(randomCardNumber));
k = (Math.round(k));
n = a;
l = k;
i = Number(i)+1;
tellTarget ("_root.placeholder" add n) {
gotoAndStop ("_root.placeholder" add n + ".card" add l);
trace ("The place holder " + n + " is choosing card number "+l);
// eliminating the duplicate factor
eval (randomPlaceholder).splice (a,1);
}
}
[Edited by uberflash on 02112001 at 12:41 AM]

placeholder = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
randomnum=Math.floor(Math.random()*placeholder.len gth)
placeholder.slice(randomnum)
Hope that helps!

Ah Finally.... tks for the help but I figured it before I got it. Here's what I did.
Basically I run through an array with a for loop.
During that loop I generate a random number(a) and use that number for a new card. Now what happens from here is I get the X and Y cords. and swap the two cards. Works really well and don't have to worry about the splice. I'll try that next time.
// Generate Random Locations for cards
// define array
placeholder = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
// define elements in the array
// x and y cordinates are by the center of the image NOT the upper left corner
placeholder[1] = _root.placeholder1;
placeholder[2] = _root.placeholder2;
placeholder[3] = _root.placeholder3;
placeholder[4] = _root.placeholder4;
placeholder[5] = _root.placeholder5;
placeholder[6] = _root.placeholder6;
placeholder[7] = _root.placeholder7;
placeholder[8] = _root.placeholder8;
placeholder[9] = _root.placeholder9;
placeholder[10] = _root.placeholder10;
placeholder[11] = _root.placeholder11;
placeholder[12] = _root.placeholder12;
placeholder[13] = _root.placeholder13;
placeholder[14] = _root.placeholder14;
placeholder[15] = _root.placeholder15;
placeholder[16] = _root.placeholder16;
placeholder[17] = _root.placeholder17;
placeholder[18] = _root.placeholder18;
placeholder[19] = _root.placeholder19;
placeholder[20] = _root.placeholder20;
// initialize variables
a = "";
PlaceHolderLength = placeholder.length;
// Generate Random loop
for (i=1; i<21; i++) {
a = (20*Math.random(PlaceHolderLength));
a = (Math.round(a));
// placeholder swap
// get X and Y cordinates
currentPlaceholderX = (getProperty(placeholder[i], _x));
//trace ("currentPlaceholderX is "+currentPlaceholderX);
currentPlaceholderY = (getProperty(placeholder[i], _y));
//trace ("currentPlaceholderY is "+currentPlaceholderY);
newPlaceholderX = (getProperty(placeholder[a], _x));
//trace ("newPlaceholderX is "+newPlaceholderX);
newPlaceholderY = (getProperty(placeholder[a], _y));
//trace ("newPlaceholderY is "+newPlaceholderY);
// move cards to the location
setProperty (placeholder[i], _x, newPlaceholderX);
setProperty (placeholder[i], _y, newPlaceholderY);
setProperty (placeholder[a], _x, currentPlaceholderX);
setProperty (placeholder[a], _y, currentPlaceholderY);
trace ("Random placeholder "+i+" has now been set to placeholder "+a);
}
tellTarget ("_root.timer") {
play();
}
Uberflash
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
