Lets say I want a mouse click event listener to trace (console print) the x coordinate of a sprite clicked. this is what Ive got, which is wrong.
public function field()
{
....
//nodes is an array containing node sprite objects
for(i = 0; i < nodes.length; i++){
var single_node:Node;
single_node = nodes[i];
single_node.addEventListener(MouseEvent.CLICK, onNodeClick)
}
...
}
private function onNodeClick(e:MouseEvent):void{
trace(this.x); // should output single_node object x coordinate
}
I believe this.x is referring to the classes property x, however i would like to refer to the single_node object.
currently console is outputting 0, regardless of what sprite i click.
Use
e.target.xEventhas atargetproperty which represents the object you’ve added an event listener to.A side note though, I’d be more inclined to have the listeners set up in your own Node class and trace the
xproperty from there.