Forum Topic: Detecting unknown movieclip

(88 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

ElDebugger

Reply To Post Reply & Quote

Posted at: 6/20/09 02:20 PM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

[Flash 8 PRO, Actionscript 2.0]

I have a stage full of 250 different movieclips. Each movieclip is a rabbit that has a different name. My code is in a frame on the main timeline. There is a dynamic text box that will display the name of a rabbit when the mouse rolls over it. I can do it like this:

rabbit1.onRollOver = function() {
	textBox=  this._name;
}

...but I would have to write this code 250 times, for each of the movieclips. I want to do it using "modular code", where I only write a single function that handles all movieclips. However, I don't know any code for leaving the name of the movieclip an unknown variable.

rabbit1.onRollOver = function() {
	textBox=  this._name;
}
rabbit2.onRollOver = function() {
	textBox=  this._name;
}
rabbit3.onRollOver = function() {
	textBox=  this._name;
}
....and on and on until 250 are written!!!

This method would be possible, but way impractical. What if I want to make changes later? I would have to edit the code in 250 different places! Holy moley!

("whatever MC your mouse is over right now").onRollOver = function() {
     textBox= this._name;
}

This one is my intention. I would only write this code once. You can see how simple it is to maintain.

Anybody know how I can do this? Thanks in advance.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 6/20/09 02:29 PM

Yambanshee DARK LEVEL 10

Sign-Up: 10/05/08

Posts: 1,427

could try this:

onMouseMove = function () {
for(var i:Number = 1; i<250; i++){
if(_root["rabbit"+i].hitTest(_xmouse,_ymouse, true)){
//actions
}
}
}

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

ElDebugger

Reply To Post Reply & Quote

Posted at: 6/20/09 02:33 PM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

Interesting. So just make a loop that iterates through all the numbers which represent the different rabbits. Make the function loop through all the names until it hits one.

Okay. I'll give it a shot.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 6/20/09 02:36 PM

Yambanshee DARK LEVEL 10

Sign-Up: 10/05/08

Posts: 1,427

pretty much. Easiest way I could think up

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

UnknownFury

Reply To Post Reply & Quote

Posted at: 6/20/09 02:50 PM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,031

Or something like:

for(var i:Number = 1; i <= 250; i++)
{
	_root["rabbit"+i].onRollOver = function()
	{
		// stuff
	}
}

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

ElDebugger

Reply To Post Reply & Quote

Posted at: 6/20/09 03:19 PM

ElDebugger NEUTRAL LEVEL 01

Sign-Up: 04/11/08

Posts: 176

I like this one, too. I'll see which one I prefer to use. Thanks guys.


None

asgrunt

Reply To Post Reply & Quote

Posted at: 6/20/09 04:27 PM

asgrunt NEUTRAL LEVEL 01

Sign-Up: 05/26/09

Posts: 164

250 rabbits? I guess (I hope) you didn't put them manually into your app. So you probably attached them (or duplicated mcs). That is the point where you simply need to store them into an array. Later on you loop through it and assign the necessary events to every single element. Then you don't need to use a for loop (and you don't need to know or calculate the instance names)


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 6/20/09 04:34 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,324

At 6/20/09 04:27 PM, asgrunt wrote: 250 rabbits? I guess (I hope) you didn't put them manually into your app. So you probably attached them (or duplicated mcs).

He did. They all have names. Fail.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


All times are Eastern Standard Time (GMT -5) | Current Time: 01:51 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!