2
votes

I'm new to AS3 and am trying to get the target name from a mouse click by using a simple getValue function but I am getting the error 1067: Implicit coercion of a value of type void to an unrelated type String. I'm not sure what I'm doing wrong.

var _userInput:String = new String();
_userInput = btn_0.addEventListener(MouseEvent.CLICK, getValue);

function getValue(e:MouseEvent):String{
   return e.target.name;
}

This may be a simple error I'm making but I'm not seeing what it is. Thanks in advance.

3
If you think about it, the return would go to the event dispatcher, not the string.Pier

3 Answers

3
votes

You can't actually do it that way, as addEventListener returns void not the type the listener function returns (String).

[Bindable] var targetName:String;

var _userInput:String = new String();
_userInput = targetName;
btn_0.addEventListener(MouseEvent.CLICK, getValue);

function getValue(e:MouseEvent):void{
targetName = e.target.name;
}
2
votes
btn_0.mouseChildren=false
btn_0.addEventListener(MouseEvent.CLICK, getValue);

function getValue(e:MouseEvent):void{
    trace(e.target.name);
}
0
votes

Assuming you have given your button an instance name of btn_0

var _userInput:String;

btn_0.addEventListener(MouseEvent.CLICK, getValue);

function getValue(e:MouseEvent):void{
_userInput = e.target.name;
trace(_userInput);
}