PDA

View Full Version : How to get the name of the component which has been trigged?


jfbaro
07-24-2006, 04:35 PM
Hi!

We are new to Flash/AS and we would like to know how to get the name of the component which has been trigged? We need to find out the "instance name" of it.

Something similar to that:

GetEvent(event ev){
if (ev='bt1'){
dothat();
}
if (ev='bt2'){
doanotherthing();
}

}

we know we could do that by using [instance_name].onPress = function();

but if we coud find a way of getting the instance name we would save a lot of code.

Thanks in advance

Navarone
07-24-2006, 04:47 PM
What do you mean by "trigged". Do you mean "triggered" as in activated? In either case check out the "this" keyword in the flash help files. :)

something like this:

function Circle(radius:Number):Void {
this.radius = radius;
this.area = Math.PI*Math.pow(radius, 2);
}
var myCircle = new Circle(4);
trace(myCircle.area);

jfbaro
07-24-2006, 05:28 PM
Thanks Navarone,

yeah, I meant "triggered"...thanks ;)

I will try to give you more details:

The app we are trying to write creates a lot of different components dynamically: Buttons, TextFields...etc

We have put the code (event handler) in an external AS file... and we would like to be able to get the name of the component which has received the event (press, release...).

For example, If the user press the "bt_1" or enter "tf_1" we would like to get the instance name in order to take the right action in the code, something like that:

if (event.getInstanceName().startsWith("bt_")){
...
}

if (event.getInstanceName().startsWith("tf_")){
...
}

We do not know if ActionScript is able to do that, so the experts might tell us! :)

Thanks in advance

Xeef
07-24-2006, 05:52 PM
P = function () {
switch (this._name) {
case "_mc1" :
trace("I am mc one");
break;
case "_mc2" :
trace("I am _mc2");
break;
case "_mc3" :
trace("Nana Don NOT PRESS MY !!!");
break;
}
};
_mc1.onPress = P;
_mc2.onPress = P;
_mc3.onPress = P;

Navarone
07-24-2006, 05:53 PM
I am no expert but it sound like you need to create a listener function which would "listen" for a particluar event and then pass that information on. Look up eventListeners.

arulprasad
07-27-2006, 04:39 PM
if its Flash V2 components tat you are using, evt.target should help u...