Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with AS3 event.currentTarget

Guest
May 21, 2017 May 21, 2017

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

TOPICS
ActionScript
870
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 21, 2017 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

...
Translate
LEGEND ,
May 21, 2017 May 21, 2017
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines