LoaderMax & Data Transfer between Flex and SWFs

This last week I have been doing some research for my capstone course in my major, WDMD480. Our project involves working with the CNR to take a twenty year old fish farm simulation and modernize it. To do this, we are using Flash, Flex and PHP to not only create the simulation, but an administrative panel and content management system for teachers who might use the program. My goal this week? Take a SWF and load it into Flex, then have two way communication between the two levels. I had a hard time with having this done seamlessly in Flex4, but in the end I ended up creating two way communication using both LoaderMax and custom events.

While trolling for resources to get me started, I found that I could find few, if any, real ‘tutorials’ on the subject of using LoaderMax in a Flex environment, so I figured I could expain not only how to use Flex with LoaderMax, but my way to transfer data from a SWF to Flex and vice versa.

SWF Component to be loaded into Flex

SWF Component to be loaded into Flex

To start, I created a simple FLA with a button. This button, when pushed, releases a bubbling custom event that has some data stored that can be grabbed by Flex.


And just to be complate, the code for the CustomDataEvent:

Next, I set up my Flex project. I put lots of comments in to explain each part!


Passing data to the SWF is just as easy. Inside of your Component.as file, you simply create a public function that you call from Flex. At that point, it is visible and accessable through your swfRaw variable.
After your MXML is set up, and you have your SWF inside of your source folder for your Flex project, you load it up and the magic starts working. The SWF is loaded and you get a popup from Flex saying it grabbed Data from the SWF. If you click the button, another Alert pops up saying that it caught your event listener!

1 Comments.

  1. Hello Jeff,

    nice tutorial!

    I have tested on Flash Builder 4.6 with Flex SDK 4.11

    But i wouldn’t like this MovieClip Instance.

    Because i am using only Flash Builder and i am creating 2 MXMLs like Main.swf and Component.swf

    But it doesn’t work because SWFLoader shows nothing Alert’s String by external swf.

    And i have tried ShowPanel() from external swf but it can not show when i click main app swfRaw.showPanel() and external swf public function showPanel():void { …. }

    And i can not solve sometime. How do i know when i am using prohectors Flash Builder… No Flash pro CS5.

    How do i access function from external swf?
    Object? Localconnection? or Anything class like ExternalInterface? Or another birdghe??

    Thanks for answer!

    Best regards, from Germany :) I am sorry because my english is not better because i am deaf :(

    Jens :)