I have many different Units on a map and I want to discover which one has been clicked by the mouse. I want to do something like this:
private function setupUnit(gridx:uint, gridy:uint):void {
unit = new Unit(gridx, gridy);
unit.addEventListener(MouseEvent.CLICK, onUnitClick);
}
private function onUnitClick(e:MouseEvent):void {
active_unit:Unit = e.target;
}
And later use active_unit.method(). But this throws the error “Implicit coercion of a value with static type Object to a possibly unrelated type Unit”
I’m sure there’s a super simple way to do this =P. Any recommendations?
Just cast the value to the right type or use the
askeyword.The cast will raise a runtime exception if it’s not the right type, and the
askeyword will return null: