Help cancelling an event
I have a component, mentioned in another post (attached here as well), which is
canvas component with a two image components in it: one is the "object"
itself, and the other is a "close" button.
The object has a click handler which brings it to the front of the display
list. The close button has a click handler which removes the whole object from
the parent container's display list. Both of these handlers work fine; the
problem is the debugger is throwing an error because a click on the close
button is apparently trying to bring IT to the front:
TypeError: Error #1009: Cannot access a property or method of a null object
reference.
at
com.taunton.games::GameItem/clickedMe()[/Users/Shared/WORK/CraftStylish/Ultimate
Sewing Room/flex/src/com/taunton/games/GameItem.mxml:50]
"clickedMe" is the handler that's assigned to the main object, it doesn't make
sense on the close button, hence the error. How can I prevent this?
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"
minWidth="200" minHeight="200" creationPolicy="all">
<mx:Script>
<![CDATA[
private function init():void {
itemImage.source = filePath;
this.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
this.addEventListener(MouseEvent.MOUSE_UP, dropMe);
this.addEventListener(MouseEvent.CLICK, clickedMe);
this.addEventListener(MouseEvent.MOUSE_OVER, showClose);
this.addEventListener(MouseEvent.MOUSE_OUT, hideClose);
closeBtn.addEventListener(MouseEvent.ROLL_OVER, enableClose);
closeBtn.addEventListener(MouseEvent.MOUSE_OUT, disableClose);
}
private function clickedMe(e:MouseEvent):void {
parent.setChildIndex(this, parent.numChildren-1);
clickSound.play();
}
private function deleteMe(e:MouseEvent):void {
Application.application.furniture.removeChild(e.cu rrentTarget.parent)
deleteSound.play();
}
private function showClose(event:MouseEvent):void {
closeBtn.visible = true;
this.setStyle("borderSize", 3);
}
private function hideClose(event:MouseEvent):void {
closeBtn.visible = false;
}
private function enableClose(e:MouseEvent):void {
closeBtn.addEventListener(MouseEvent.CLICK, deleteMe);
}
private function disableClose(e:MouseEvent):void {
closeBtn.removeEventListener(MouseEvent.CLICK, deleteMe);
}
]]>
</mx:Script>
<mx:Image id="itemImage" />
<mx:Image id="closeBtn" source="@Embed('assets/close.png')" visible="false"
/>
</mx:Canvas>
|