Skip to main content
Participant
December 10, 2011
Answered

use button to set a variable

  • December 10, 2011
  • 1 reply
  • 591 views

hi, can someone help me.

I would like to use a couple of buttons that I made to set a variable. I'd like to use the instance name of the button(s) as the value stored as the variable. how can i code this?

thanks.

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

You can use the currentTarget of the event to target the button and acquire its name...

var btnName:String;

btn.addEventListener(MouseEvent.CLICK, storeName);

function storeName(evt:MouseEvent):void {
     btnName = evt.currentTarget.name;

     trace(btnName); // will trace "btn"
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
December 10, 2011

You can use the currentTarget of the event to target the button and acquire its name...

var btnName:String;

btn.addEventListener(MouseEvent.CLICK, storeName);

function storeName(evt:MouseEvent):void {
     btnName = evt.currentTarget.name;

     trace(btnName); // will trace "btn"
}

kwiwiiAuthor
Participant
December 10, 2011

Thank you so much! That worked and I was trying so many things last night. I'm new to flash...

Ned Murphy
Legend
December 10, 2011

You're welcome.  It's good to try things, even when they don't work... you learn either way.