Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One more thing about randomness...

Status
Not open for further replies.

carmenMiranda

Programmer
May 22, 2003
47
GB
If you have read any of my recent posts you'll have worked out that I'm a bit of actionscript newbie who is experimenting with dynamically attached and positioned movie clips.

My latest stumbling block (and thus question) concerns the following: I have a section of code that takes one MC and places numerous instances of that MC to the stage. Rolling over those clips activates a function which references each individual clip by name (thanks Kenneth!). That works perfectly for one MC.

But what if I have a number (let's say 5, for this example) of different MC in the library and want to place instances of these at random?

I first thought of putting the 5 MC into an array, then randomly choosing a position in the array, and then passing the selected MC name to the 'nowGoAndPlaceThisMC' function.

However, I can't seem to make that work.

So my question is : is what i am trying to do conceptually correct (if so, my code construction must be *completely* wrong) or is there a different (simpler?) approach to achieve the same goal?

As always, thanks in advance for any advice with this.
 
Hi there, not sure if I get you right,
but you want multiple movieclips in your flash and do something with them?

I took a bit of code I am using in the hope you can study it and take from it that what you need.

In principle it creates a movieclip from scratch (you can replace this with an instance) within a movieclip called thumbnail_mc.

Each clip gets a character and number assigned to it so you can refer to them later (t1, t2, t3 , t4 etc).

You can also assign actions to these clips, usually using something like: this._alpha = 10% orso.

I add a listener object to check if the movie clip has fully loaded. I do this because my clips are filled with jpgs I am loading from an external source and I need to get more information about them, but can't get this until they are fully loaded.... a real pain.

eval("thumbnail_mc.t"+k) will translate to 'thumbnail_mc.t0', 'thumbnail_mc.t1', etc
This is helpful to know when you need to call these movie clips. I tried to do this before with an array, but failed because I didn't know about using 'eval'.

Well, if you need any help, just post a reply :)

Code:
for (k=0; k<10; k++) 
{
 thumbnails_fn(k);

 // lets look at the x position of each clip just as an example to see how to call them after creation

 trace(eval("thumbnail_mc.t"+k)._x);

}


function thumbnails_fn(k) 
{ 
	thumbnail_mc.createEmptyMovieClip("t"+k, thumbnail_mc.getNextHighestDepth()); 
	
	tlistener = new Object(); 
	tlistener.onLoadInit = function(target_mc) 
	{ 		
		target_mc.pictureValue = k; 
		
		target_mc.onRelease = function() 
		{ 
                  // do something if you like
		}; 

		target_mc.onRollOver = function() 
		{ 
                   // do something if you like
		}; 
		
		target_mc.onRollOut = function() 
		{ 
                   // do something if you like
		};

	}; 		

	image_mcl = new MovieClipLoader(); 
	image_mcl.addListener(tlistener); 
	image_mcl.loadClip(path+thumbnails[k], "thumbnail_mc.t"+k); 
}

JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
Hi - thanks very much for the answer, but I eventually managed to get this working using an array. I've posted the code in case it might help someone at some point in the future:
Code:
var ballList:Array = new Array("blueBall_mc", "orangeBall_mc", "pinkBall_mc", "greenBall_mc", "purpleBall_mc", "redBall_mc");

var nextBall:String;

function pickBall():Void {
	var randBall:Number;
	randBall = Math.floor(Math.random() * ballList.length);
	nextBall = ballList[randBall];
	trace(nextBall);
}

pickBall();
 
Hi, I would be interested to see what on earth you have been putting together :)
Any URL?

JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top