views:

220

answers:

1

I have a flex application and have embedded a flash (SWF) file into it using <mx:SWFLoader>. There is an "Exit" button on the Flash file. I want to be able to handle the button click event on the flex application.

So when that button in the flash file is clicked, I want to perform an action in the parent flex application. How can I do this? Thanks!

A: 

You can do this if the event from flash "bubbles". When you dispatch the event from Flash, do this:

dispatchEvent(new Event("myEventName", true)); // that 'true' for bubbles, in the constructor

Then you should be able to capture it in Flex no problem, with:

addEventListener("myEventName", handler);

... as long as addEventListener is called on a component at or above the SWFLoader.

If you can't modify the Flash SWF, or it's a complete black box, then you can just register a MouseEvent.CLICK handler with useCapture = true, and check to see if it's the right button:

swfLoader.addEventListener(MouseEvent.CLICK, swfLoader_clickHandler, true, 0, true);

protected function swfLoader_clickHandler(event:MouseEvent):void
{
    if (event.target.name == "some_way_to_identify_the_button")
        // do X
}

Hope that helps, Lance

viatropos
@viatroops: hey, this didn't work. I do have access to the fla. The fla executes this line of code `dispatchEvent(new Event("myEvent"), true));` and I have a `swfloader_completeHandler` event handler on the SWFLoader in Flex. In this method, I have `swfloader.addEventListener("myEvent", nextStepFunction);`. But now, when I click on the exit button in the swf file (when it is embedded in the flex app), nothing happens. Did I do something wrong?
aip.cd.aish
try it so dispatchEvent looks like this: `dispatchEvent(new Event("myEvent", true));` instead of this `dispatchEvent(new Event("myEvent"), true))`, and try to set useCapture on the listener to true, like this: `addEventListener("myEventName", handler, true, 0, true);`. Let me know if that works.
viatropos
@viatroops. Oops, that was a typo. Yes, that is what I had for dispatchEvent. I tried the second addEventListener, and that did not work either.
aip.cd.aish
I think I found the problem. The FLA file is based on "Action Script 2.0", which does not support Event bubbling. I'll check with the other developer to see if he can upgrade the project to AS3. If not is there some other way?
aip.cd.aish
I've never used actionscript 2. Maybe try dispatching it from the stage, or from some other more reliable places, see if that works. you may need to use a javascript intermediate if as2 and as3 are not able to communicate at all, check this out: http://www.gskinner.com/blog/archives/2007/07/swfbridge_easie.html.
viatropos