Skip to main content
May 21, 2017
Answered

Help with AS3 event.currentTarget

  • May 21, 2017
  • 1 reply
  • 969 views

I have this code

for (var i = 0; i < levelbtns.length; i++) {

if (event.currentTarget== levelbtns) {

}

levelbtns contains a whole heap of buttons

It requires the name check because the resulting action depends on a few variables that i need the button name to know.

I’m getting the error of

1120: Access of undefined property event.

Thanks

This topic has been closed for replies.
Correct answer Ned Murphy

If you do not have that "event" defined as an incoming argument of the function that contains that code then targeting it is not going to work.  The usual scenario for something like you appear to want would be first having event listeners assigned to the buttons, then having the event handler function checking for which button was clicked.

for (var i = 0; i < levelbtns.length; i++) {

       levelbtns.addEventListener(MouseEvent.CLICK, clicked);

}

function clicked(event:MouseEvent):void {

      var btnName:String = event.currentTarget.name;

}

Note that you do not need to loop thru all the buttons to get the button name.  The event.currentTarget gives you a direct line of sight as to which button was clicked.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
May 21, 2017

If you do not have that "event" defined as an incoming argument of the function that contains that code then targeting it is not going to work.  The usual scenario for something like you appear to want would be first having event listeners assigned to the buttons, then having the event handler function checking for which button was clicked.

for (var i = 0; i < levelbtns.length; i++) {

       levelbtns.addEventListener(MouseEvent.CLICK, clicked);

}

function clicked(event:MouseEvent):void {

      var btnName:String = event.currentTarget.name;

}

Note that you do not need to loop thru all the buttons to get the button name.  The event.currentTarget gives you a direct line of sight as to which button was clicked.