# Load MovieClip at random, but only once

**URL:** https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406
**Category:** flash
**Created:** [March 13, 2003, 9:12pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406 "2003-03-13T21:12:24Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Indianos](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Indianos](https://forum.kirupa.com/u/Indianos)
#### Post date: [March 13, 2003, 9:12pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/1 "2003-03-13T21:12:24Z")

</div>

Ok, this is the case:

I’ve got 12 MovieClips with the following linkage identifiernames:

“lbrboven”, “ldrboven”, “lflboven”, “lfrboven”, “lgeboven”, “lgrboven”, “lliboven”, “lnhboven”, “lovboven”, “lutboven”, “lzeboven” and “lzhboven”.

I want them to appear on screen AT RANDOM. But each MC has to appear just one time. If all 12 have appeared (each time you start the application, the appearance is in a different order), the appearance have to stop and the application has to go to another frame.

How do I need to this?

Someone told me to use attachmovie method and using arrays. Something with this:

clips = [“MC1”, “MC2”, “MC3” … “MC12”]

randMC = clips.splice(random(clips.length),1);

if (clips.length){  
randMC = clips.splice(random(clips.length),1);  
}

Can someone help me with this and give me the exact script for this?

Thanks in forward!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 4:54am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/2 "2003-03-14T04:54:43Z")

</div>

Howdy…

I have used Number.getUniqueRandom by Stickman from layer51… I think this should get you started… 😉

```auto
Number.getUniqueRandom = function(number)
{
	var numberlist = new array();
	for (count = 1; count <= number; count++)
	{
		numberlist[count] = count;
	}
	var randomlist = new array();
	for (count = numberlist.length - 1; count > 0; count--)
	{
		picked = math.floor(math.Random() * count) + 1;
		randomlist[count - 1] = numberlist[picked];
		numberlist[picked] = numberlist[count];
	}
	return randomlist;
};

clipArray = new Array("lbrboven", "ldrboven", "lflboven",
"lfrboven", "lgeboven", "lgrboven",
"lliboven", "lnhboven", "lovboven",
"lutboven", "lzeboven", "lzhboven");
mynumbers=Number.getUniqueRandom(clipArray.length)

for (i = 0 ; i < mynumbers.length ; i++)
{
	trace("mynumbers[" + i + "] = " + mynumbers*
                          + " clip name : " + clipArray[mynumbers* - 1]);
}

```

> mynumbers[0] = 4 clip name : lfrboven  
> mynumbers[1] = 2 clip name : ldrboven  
> mynumbers[2] = 6 clip name : lgrboven  
> mynumbers[3] = 7 clip name : lliboven  
> mynumbers[4] = 10 clip name : lutboven  
> mynumbers[5] = 1 clip name : lbrboven  
> mynumbers[6] = 5 clip name : lgeboven  
> mynumbers[7] = 11 clip name : lzeboven  
> mynumbers[8] = 12 clip name : lzhboven  
> mynumbers[9] = 3 clip name : lflboven  
> mynumbers[10] = 8 clip name : lnhboven  
> mynumbers[11] = 9 clip name : lovboven

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 10:57am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/3 "2003-03-14T10:57:19Z")

</div>

Tnx for your help Cyanblue! 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 11:19am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/4 "2003-03-14T11:19:39Z")

</div>

No problem… Don’t take it personally… 😉  
Please don’t post it over the forums… You just wasted either mine or pinkaboo’s time for this… I wouldn’t have posted what I have if I knew that pinkaboo was helping you… Know what I mean??? 😛

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 11:22am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/5 "2003-03-14T11:22:33Z")

</div>

OK, I get it, but on the other hand, you both gave different types of code. By this way, I’ll learn both ways. So i’m thankfull to both you guys.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 11:24am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/6 "2003-03-14T11:24:49Z")

</div>

Yup… I know… That’s why I said don’t take it personally… 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 2:17pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/7 "2003-03-14T14:17:44Z")

</div>

Ah, ok 🙂

Where do I have to put “mynumbers[0] to [11]”?

What’s the reason you’ve quoted them, instead of putting it with actionscript above?

As output, I get the list of those numbers. But in de application self, no movieclips appear.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 2:39pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/8 "2003-03-14T14:39:49Z")

</div>

That last quote is the result output trace…

I don’t know how you have set things up in your Flash movie, so I just have it print out the possible random movieclip names… You need to use that data or the data that pinkaboo provided to fit your movie… All me or pinkaboo provided is the way to create an array which is one unique sets of random movieclip name which you can use in your movie…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 2:49pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/9 "2003-03-14T14:49:15Z")

</div>

Aha.

I tried your way and I get the output screen. The random function is really working. No i’m trying to get the MC’s on stage, which have to be at random. I only don’t get further than the output screen…

Pinkaboo’s code als works fine and I also get the MC’s onscreen. So I need to combine some of his code with yours. I understand your code more than his code… but i’m still learning :bandit:

Tnx for the help pal!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 3:11pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/10 "2003-03-14T15:11:11Z")

</div>

No problem… Shoot the problem if you need some more help on how to combine two… 😉

Oh… pinkaboo might be looking for you at the moment… pinkaboo is not HE but SHE if I know her correctly… So, you better run away while you can… =)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 3:34pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/11 "2003-03-14T15:34:16Z")

</div>

He, she, verry difficult to tell on the Internet 😉

But ehm, why is she looking for me? I don’t see any messages on the other forum.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 14, 2003, 4:52pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/12 "2003-03-14T16:52:56Z")

</div>

I used Pinkaboo’s actionscript to load the MC’s at random. Works fine, when I use the script on the main stage.

The MC’s which appear, appear al over the place on screen. So I want them to be loaded in a frame/border. I want them to load in a other MovieClip, and that movieclip I’ll put on the mainscreen. The MC’s will load within that movieclip, and not elsewhere on stage (I think…).

But when I try to do this, nothing happend. The output screen now doesn’t show the different MC’s names as it supposed to be. Instead the text ‘undefined’ showed up.

Do you know what this means? I think the ‘link’ to the instancename of the MC’s aren’t right anymoren. (when I delete the movieclips from my library, I got the same text in my output screen)

Now I use this pieces code in the actionscript:

\_root.mymashArray = [“lbrboven”, blablabla]  
mymashArray.shuffle(\_root.mashremix=new Array());

etc etc

Do I need to change something with that \_root stuff?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 15, 2003, 9:05am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/13 "2003-03-15T09:05:03Z")

</div>

She’s looking for you because you called her HE… I was just joking… =)

Can you post your file or code??? I can’t really see what’s wrong with that…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 15, 2003, 6:41pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/14 "2003-03-15T18:41:52Z")

</div>

I see 🙂

The code is as Pinkaboo gave:

* * *

//randomly shuffles array and returns as custom defined new array

Array.prototype.shuffle = function(secondArray) {  
interimArray = new Array();  
arrayCount = ((this).length);  
deleteCount = ((secondArray).length);  
secondArray.splice(0, deleteCount);  
//copies values from original array into interim array so that original data is not lost  
for (a=0; a\<arrayCount; a++) {  
value = this[a];  
interimArray.splice(a, 0, value);  
}  
//chooses random array data \* copies into first postition in new array,  
// deletes that data from interim array so it can’t be chosen again  
for (a=0; a\<arrayCount; a++) {  
num = (random(interimArray.length));  
value = interimArray[num];  
secondArray.splice(1, 0, value);  
interimArray.splice(num, 1);  
}  
};  
//sample array to shuffle

\_root.mymashArray = [“lbrboven”, “ldrboven”, “lflboven”,  
“lfrboven”, “lgeboven”, “lgrboven”,  
“lliboven”, “lnhboven”, “lovboven”,  
“lutboven”, “lzeboven”, “lzhboven”];

//eg:  
mymashArray.shuffle(\_root.mashremix=new Array());  
trace(mashremix);

i = 0;  
\_root.onMouseDown = function() {  
//checks to see if i isn’t higher than the length of the array  
if (i\<mashremix.length) {  
//if it isn’t utilises i to take that linkage name and attach the appropriate mc  
\_root.attachMovie(mashremix\*,“newName”,depth);  
trace("attach\_or\_whatever\_to\_add: "+mashremix\*);  
//increments i  
i++;  
//else, if i is the same as the length of the array, perform an action:  
} else {  
trace(“action\_to\_go\_to\_next\_frame\_here”);  
}  
};

* * *

It works fine if I paste this code in de actionscript area of a frame.  
But when I put this code in a MovieClip, it doens’t work… and get “undefined” as message in my output screen.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 8:33am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/15 "2003-03-16T08:33:27Z")

</div>

Well… I just tried the code in my machine and that didn’t work for me… There were several errors in there… Maybe something happened while you were pasting it???

Anyways… Yes… The code is supposed to be run as a frame script, not the movieclip script… Why do you need to put the code into the movieclip??? When you say

> But when I put this code in a MovieClip, it doens’t work…  
> Are you talking about the movieclip script or the frame script inside of the movieclip??? (Huge difference in between two…)

I think this should be done first… Me or pinkaboo provided you a code so that you can disect the code and understand it so that you can break it apart and reassemble it to your liking… What do you think??? Do you think you did that, or are you just copying it and pasting it hoping that the code will do the job for you???

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 11:20am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/16 "2003-03-16T11:20:04Z")

</div>

The code as is stands like above, is the right code. When I paste it back in Flash, it works fine.

I meant a frame script. When I put it in a frame script, say, just after opening a new untitled flash file, (and copy the necessary MC’s: lbrboven, ldrboven, etc in the library) it works.

When I make a draw a square or something, convert that to a MovieClip, edit it, and in the first frame pasting the script… then the script doens’t work as it supposed to be. I got that ‘undefined’ error.

The reason why I want it in a MovieClip (that square, which doens’t take whole the stage/screen) is, that when I don’t use a MovieClip and just put the script in the framescreen of the ‘mainstage’, the MovieClips which appear at random by the script, appear all over the place. The first somewhere left in corner, the other somewhere else. You know what I mean?  
By putting it in a MovieClip, I want the appearance restricted to the size of that MovieClip. As like a window, within the other movieclips (by the script) are being loaded in.

The code you and pink gave me, has two reasons for me. I’m a beginner, so I’m looking for a piece of code with some functions I need. By myself I never can program that code. I need examples. I will learn from that code.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 11:27am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/17 "2003-03-16T11:27:16Z")

</div>

Well… Like I said, there are several things missing in the code… Can you copy and paste the code again???  
I get this error messages…

> Scene=Scene 1, Layer=Layer 1, Frame=1: Line 10: ‘;’ expected  
> for (a=0; a value = this[a];

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 15: ‘;’ expected  
for (a=0; a num = (random(interimArray.length));

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 19: Unexpected ‘}’ encountered  
}

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 11:35am UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/18 "2003-03-16T11:35:26Z")

</div>

This is the code. I just copied and pasted from my flash file. (and tested it,… it works)

//randomly shuffles array and returns as custom defined new array

Array.prototype.shuffle = function(secondArray) {  
interimArray = new Array();  
arrayCount = ((this).length);  
deleteCount = ((secondArray).length);  
secondArray.splice(0, deleteCount);  
//copies values from original array into interim array so that original data is not lost  
for (a=0; a\<arrayCount; a++) {  
value = this[a];  
interimArray.splice(a, 0, value);  
}  
//chooses random array data \* copies into first postition in new array,  
// deletes that data from interim array so it can’t be chosen again  
for (a=0; a\<arrayCount; a++) {  
num = (random(interimArray.length));  
value = interimArray[num];  
secondArray.splice(1, 0, value);  
interimArray.splice(num, 1);  
}  
};  
//sample array to shuffle

\_root.mymashArray = [“lbrboven”, “ldrboven”, “lflboven”,  
“lfrboven”, “lgeboven”, “lgrboven”,  
“lliboven”, “lnhboven”, “lovboven”,  
“lutboven”, “lzeboven”, “lzhboven”];

//eg:  
mymashArray.shuffle(\_root.mashremix=new Array());  
trace(mashremix);

i = 0;  
\_root.onMouseDown = function() {  
//checks to see if i isn’t higher than the length of the array  
if (i\<mashremix.length) {  
//if it isn’t utilises i to take that linkage name and attach the appropriate mc  
\_root.attachMovie(mashremix\*,“newName”,depth);  
trace("attach\_or\_whatever\_to\_add: "+mashremix\*);  
//increments i  
i++;  
//else, if i is the same as the length of the array, perform an action:  
} else {  
trace(“action\_to\_go\_to\_next\_frame\_here”);  
}  
};

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 12:08pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/19 "2003-03-16T12:08:03Z")

</div>

The code still is not working, so I had to run down to [ActionScript.org](http://ActionScript.org) to get the code from the thread…

Mainly the reason why the code didn’t work within the movieclip is because of the use of ‘\_root’… Check out the pathing tutorials/section to get more information regarding it…

```auto
//randomly shuffles array and returns as custom defined new array
Array.prototype.shuffle = function(secondArray)
{
	interimArray = new Array();
	arrayCount = ((this).length);
	deleteCount = ((secondArray).length);
	secondArray.splice(0, deleteCount);
	//copies values from original array into interim array so that original data is not lost
	for (a = 0; a < arrayCount; a++)
	{
		value = this[a];
		interimArray.splice(a, 0, value);
	}
	//chooses random array data * copies into first postition in new array,
	// deletes that data from interim array so it can't be chosen again
	for (a = 0; a < arrayCount; a++)
	{
		num = (random(interimArray.length));
		value = interimArray[num];
		secondArray.splice(1, 0, value);
		interimArray.splice(num, 1);
	}
};
//sample array to shuffle
mymashArray = ["boiled", "chipped", "fried", "bubbled", "sliced", "mashed", "baked", "roasted"];
//eg:
mymashArray.shuffle(mashremix = new Array());
trace(mashremix);
i = 0;
_root.onMouseDown = function()
{
	//checks to see if i isn't higher than the length of the array
	if (i < mashremix.length)
	{
		//if it isn't utilises i to take that linkage name and attach the appropriate mc
		_root.attachMovie(mashremix*, "newName", depth);
		trace("attach_or_whatever_to_add: " + mashremix*);
		//increments i
		i++;
		//else, if i is the same as the length of the array, perform an action:
	}
	else
	{
		trace("action_to_go_to_next_frame_here");
	}
};

```

I tested this on my machine and it works just fine… Let me know… Please use [code formatting](http://www.kirupaforum.com/forums/misc.php?action=bbcode#buttons) next time when you post the code… 😉

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 16, 2003, 12:12pm UTC](https://forum.kirupa.com/t/load-movieclip-at-random-but-only-once/15406/20 "2003-03-16T12:12:21Z")

</div>

Ah… code formatting text. Ok, got that 🙂

Yes, that \_root stuff. I thought so (see a previous reply). Than it must be that, cause the script itself works fine.
