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

How do I extract a button's name from its event variable?

Explorer ,
Sep 03, 2009 Sep 03, 2009

In the code below, the trace gives the instance name, not the button name.

How do I get the button name?

var b1:Button = new Button();

b1.move(400,100);

b1.label = "B1";

b1.addEventListener(MouseEvent.CLICK,buttonHandler);addChild(b1)

function buttonHandler(event:MouseEvent):void

     var button:Button = event.target as Button

     trace(button.name)

}

TOPICS
ActionScript
745
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 , Sep 03, 2009 Sep 03, 2009

I don't know if it is not updated or stands as is, but the Button component's name is its instance name, possibly unlike other objects in AS3 where the instance name and the name (just a property) you assign are two diffferent things.

I don't see where you assign a name, so the name that gets generated would be the instance name in any case.

Translate
LEGEND ,
Sep 03, 2009 Sep 03, 2009

I don't know if it is not updated or stands as is, but the Button component's name is its instance name, possibly unlike other objects in AS3 where the instance name and the name (just a property) you assign are two diffferent things.

I don't see where you assign a name, so the name that gets generated would be the instance name in any case.

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
LEGEND ,
Sep 04, 2009 Sep 04, 2009

Ned is correct. You haven't given the button a name. You have created a variable (b1) that refers to the button, but that is not the same as the name property of the button. As far as I know you cannot get the "b1" back, but there is no need to do so.

e.target will give you the reference to the button and everything that it has.

What are you thinking you need to do?

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
Explorer ,
Sep 05, 2009 Sep 05, 2009
LATEST

Rothrock,

I am sharpening my skills with the MCV pattern. There are several buttons in my View say b1, b2, b3, etc.

Notice that in View I can do switch (button) and case b1 : where b1 is a button variable. This is because the button objects are 'visible' in View.

However when View passes the event variable, button to Controller, I cannot do switch (button) in Controller because the button objects are not visible there.

However I can do  switch (button.name)  and case "b1" : where the 'literal' b1 is the name of the button.

Extract of View:

public class View extends CompositeView {

    protected var b1:Button;

    public function View(aModel:IModel, aController:IMouseClickHandler, target:Stage) {

          super(aModel, aController);
          b1 = new Button();

          b1.name = "b1";

          b1.label = "B1";

          b1.addEventListener(MouseEvent.CLICK, this.onMouseClick);

          addChild(b1)

    }

    private function onMouseClick(event:MouseEvent):void {
          var button:Button = event.target as Button;
          switch (button){
              case b1:
                    break;
          }

          // delegate to the controller (strategy) to handle it
          (controller as IMouseClickHandler).flagsSetter(flag,event);
    }

}

Extract of Controller:

public class Controller implements IMouseClickHandler {

    private var model:IModel;

    public function Controller(aModel:IModel) {
          this.model = aModel;
    }
    public function mouseClickHandler(event:MouseEvent):void {
          model.setScene(event); // update model
    }
    public function flagsSetter(flag:Boolean,event:MouseEvent):void {
          var button:Button = event.target as Button;
          switch (button.name){
              // update model
              case "b1":

                        model.setResourcesFlag(flag);

                        break;

          }

}

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