Getting Flex and Flash to play nice to each other, when they are not on the same server.

Posted by on August 25, 2008

Ok, so recently I needed to get a Flash swf into a Flex project and have the two talk to each other. Ok, fairly normal, I did it for a page navigation scroll bar that was AS2 and got it to talk with our Flex project and it works great. Problem though, the Flash swf I need to bring in will exist on another server, and the scroll bar I had worked on, we embedded into the project.

I spent days working on this. Why?

Well, two things:

  1. We get these files from a design firm and I wanted to not change their code that we gave them earlier.
  2. Cross domain issues.

I tried everything I could think of to bust this open, searched around, and came up with the solution. I had to change the code that the design firm used, moving them from AS2 to AS3.

Sean the Flex Guy has an excellent and simple tutorial for doing half of what I needed. It loads in a Flash swf animation, and then Flex talks to it to move the animation forward frame by frame. So that was covered, at least once I made the decision to move to AS3.

The other half, Flash talking with Flex wasn’t covered, but I found a solution in the Adobe Docs. As long as both the Flex and Flash swfs have their Security.allowDomain set properly, then the two can communicate back and forth (this needs to be set for the Flex to Flash work too). What I found out, was that events, such as Mouse events, will bubble if this is set properly. So all I needed to do was to listen for the MouseEvent that was fired when the buttons in the Flash swf were clicked. The rest was easy.

I imagine that if there is something else that happens in the Flash swf, you can create a custom event for it, and listen for that in the Flex swf. Since there are more things in Flash and Flex than are controlled buy your MouseEvents Horatio.

Powered by ScribeFire.