Copy link to clipboard
Copied
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)
}
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
}
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more