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!

Controlling dynamically generated movieclips 1

Status
Not open for further replies.

carmenMiranda

Programmer
May 22, 2003
47
GB
I have written some code to dynamically place a variable number of balls (movieclips) onscreen and dynamically name them. So far, so good.

I also have a function to move an individual ball downwards (thus 'dropping' it out of the bottom of the stage).

I want a user to be able to rollover any ball and that ball to begin dropping.

How do I get the name of the particular ball movieclip they're currently rolling over and pass that name to the 'drop' function, without writing some code that sits on the movieclip (I want all my code on the main timeline)?

I'd really appreciate some advice. Thanks.
 
Try something like this:
Code:
for (var i = 0; i<10; i++) {
	var mc:MovieClip = this.attachMovie("mcX", "mcX"+i, i+1, {_x:15+30*i, _y:100});
	mc.onRollOver = function() {
		trace(this);
	};
}
Trace gives you the MovieClip instance.

Kenneth Kawamoto
 
Well thanks for the quick response - but that isn't working how I intended. When one ball is rolled over, all the balls drop together, not one at a time (as they are rolled over).

Here's the code:

Code:
//function to attach necessary number of balls in each row
function populateRow(j) {
		//attach numBalls balls for each row
		for (var i:Number = 0; i < numBalls; i++) {
			//name each block dynamically as it is created
			var ballName:String = "ball" + j + i + "_mc";
			//create new X co-ord to position each block 
			var ballXstart:Number = Math.round(ballSpacing * (i + 1));
			//trace(ballXstart);
			var k = j + i * (numRows);
			//attach block, assign unique name and depth according to iteration of loop
			this.attachMovie("ball_mc", ballName, k);
			//call random colour function
			randColour(ballName);
			//add the name of the newly created block to the storage array
			ballArray.push(this[ballName]);
			//set ball diameter
			this[ballName]._width = this[ballName]._height = ballDiam;
			//set X position of each block
			this[ballName]._x = ballXstart;
			//set Y position of each block
			this[ballName]._y = rowYstart;
			//track block depths as they are added
			nextDepth = this[ballName].getDepth();
			trace(ballName);
			[COLOR=red]//add rollover functionality
			this[ballName].onRollOver = function() {
				dropBall();
			}[/color red]
	}
	//increment depth counter by one
	nextDepth++;
}

From this, is it possible to see what I'm doing wrong? Or do you need to see more of the code?
 
It kind of depends on your "dropBall()" function but you should pass the MovieClip reference to "dropBall()" function:
Code:
...
this[ballName].onRollOver = function():Void  {
	dropBall(this);
};
...
Then in the "dropBall()" function you would do something like:
Code:
function dropBall(target:MovieClip):Void {
	target.onEnterFrame = function():Void  {
		this._y++;
	};
}

Kenneth Kawamoto
 
Thanks very much, that worked a treat. The final dropBall function looks like this:
Code:
function dropBall(target:MovieClip) {
	target.onEnterFrame = function() {
		if (this._y < (Stage.height + ballDiam)) {
			this._y += Math.sin(startNum) * range;
			startNum += rate;
		}
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top