Load MovieClip at random, but only once

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!

Howdy…

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

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

Tnx for your help Cyanblue! :slight_smile:

No problem… Don’t take it personally… :wink:
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??? :stuck_out_tongue:

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.

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

Ah, ok :slight_smile:

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.

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…

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!

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

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… =)

He, she, verry difficult to tell on the Internet :wink:

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

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?

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…

I see :slight_smile:

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.

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???

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.

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
}

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”);
}
};

The code still is not working, so I had to run down to 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…

//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 next time when you post the code… :wink:

Ah… code formatting text. Ok, got that :slight_smile:

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