Darko74
07-15-2009, 09:10 AM
I have two buttons which are two MC instances of the flash library, whose purpose is to zoom in and zoom out an image respectively. They are initially on the stage (from edition time). Their names: bZoomIn, bZoomOut respectively.
I decided to assign the same event handler to both buttons, and inside this function, discriminate between them to apply their proper respective actions. And this decision, supposedly easy to handle, is driving me nuts! :o
This is the code. Please ignore those BBcode tags. I can't get rid of them without messing up the code again (this editor is a bit buggy):
bZoomIn.addEventListener(MouseEvent.CLICK, scalePic);
bZoomOut.addEventListener(MouseEvent.CLICK, scalePic);
[COLOR=black]function scalePic(e:MouseEvent):void {
var currentButton:InteractiveObject = e.target as InteractiveObject;
trace(currentButton, InteractiveObject(bZoomOut), InteractiveObject(bZoomIn)); // traces: [object SimpleButton] [object MovieClip] [object MovieClip]
if (currentButton == getChildByName("bZoomIn") as InteractiveObject) {
trace("Zoom in!!");
} else if (currentButton == getChildByName("bZoomOut") as InteractiveObject) {
trace("Zoom out!!");
}
}
None of the conditions is true. So it does nothing.
I first tried to force type cast currentButton to MovieClip (the two buttons are movieclips in the library, so I thought it would work) but it returned null. They are treated as SimpleButton instances. So I decided to do just the opposite: force cast type bZoomIn/bZoomOut as SimpleButton, just like e.target is, but they also returned null. So I finally thought to force both sides type conversion as their first common ancestor, InteractiveObject. I thought this way they would be equivalent in the conditional. I was wrong.
I don't know what else to do. Can somebody help me? I don't want to give in and resort to separate event handlers.
I decided to assign the same event handler to both buttons, and inside this function, discriminate between them to apply their proper respective actions. And this decision, supposedly easy to handle, is driving me nuts! :o
This is the code. Please ignore those BBcode tags. I can't get rid of them without messing up the code again (this editor is a bit buggy):
bZoomIn.addEventListener(MouseEvent.CLICK, scalePic);
bZoomOut.addEventListener(MouseEvent.CLICK, scalePic);
[COLOR=black]function scalePic(e:MouseEvent):void {
var currentButton:InteractiveObject = e.target as InteractiveObject;
trace(currentButton, InteractiveObject(bZoomOut), InteractiveObject(bZoomIn)); // traces: [object SimpleButton] [object MovieClip] [object MovieClip]
if (currentButton == getChildByName("bZoomIn") as InteractiveObject) {
trace("Zoom in!!");
} else if (currentButton == getChildByName("bZoomOut") as InteractiveObject) {
trace("Zoom out!!");
}
}
None of the conditions is true. So it does nothing.
I first tried to force type cast currentButton to MovieClip (the two buttons are movieclips in the library, so I thought it would work) but it returned null. They are treated as SimpleButton instances. So I decided to do just the opposite: force cast type bZoomIn/bZoomOut as SimpleButton, just like e.target is, but they also returned null. So I finally thought to force both sides type conversion as their first common ancestor, InteractiveObject. I thought this way they would be equivalent in the conditional. I was wrong.
I don't know what else to do. Can somebody help me? I don't want to give in and resort to separate event handlers.