FlashCanon Flash Platform stuff from Jason Fincanon

29Sep/0814

Making Flash and Flex talk (Part 1)


I recently ran into some issues making Flex talk to a loaded swf while also allowing that loaded swf to talk to its Flex parent. I had done it once before on a prior project but since it was a one-time deal, I had forgotten exactly what I did to accomplish the task. I knew it had to do with casting the SWFLoader.content as a MovieClip, but I still needed to go digging back to the old project to make sure I had all of the correct steps. That was when problem #2 showed up... I couldn't remember which project I did this in. So, as the next logical step, me and a friend started doing some research online and found less than satisfactory results/answers to our questions.This lead me to feeling the need to write this post not only for my own future reference but also in hopes that it may show up in someone else's search results and help them in the future.

This example involves a total of 3 files: FF_Talk_Simple.mxml, ffTalkSimpleSwf.swf and, of course ffTalkSimpleSwf.fla.
You can view the source code either here or by right-clicking on the example and clicking "View Source". You can also grab the zip containing these files via the download link at the end of this post.

THE CODE:
Let's take a look at the source and break it down just a little.

FLEX SIDE:
In the mxml file, I start by declaring a couple of variables (flashSaid and mySwfMc).
flashSaid will be used to populate a dynamic text field so you can see that your string has actually made it from Flash to Flex and mySwfMc will be used as a reference to your loaded swf child.

Actionscript:
  1. [Bindable]
  2. private var flashSaid:String = "";
  3. private var mySwfMc:MovieClip;

Next, is a function named setSwfMc. This function is called from the SWFLoader when your child swf has finished loading. Once called, setSwfMc does two things. First, it casts the content of the SWFLoader as a MovieClip (this is required in order to access everything inside the child swf). Next, it passes a reference to the Flex parent into the child swf (this will be covered in the Flash code below).

Actionscript:
  1. private function setSwfMc():void{
  2.     mySwfMc = mySWFLoader.content as MovieClip;
  3.     mySwfMc.myFlexParent = this;
  4. }

The other two functions in the mxml file handle the communication on this end of the conversation. They are named listenToFlash and talkToFlash and you can probably guess what they do pretty quickly. listenToFlash will be called from the child swf and it is expecting a to recieve a string. Once called, it takes that string and assigns it as the value of the flashSaid variable mentioned earlier. Since that variable is [Bindable], the text field is updated and you know it worked. The other function here, talkToFlash, is very similar but it is sending instead of receiving. Within talkToFlash, we are calling a function named listenToFlex that resides in the child swf. Much like the listenToFlash functon, listenToFlex is expecting a string to be passed.

Actionscript:
  1. public function listenToFlash(stringToShow:String):void{
  2.     flashSaid = stringToShow;
  3. }
  4.  
  5. private function talkToFlash(stringToPass:String):void{
  6.     mySwfMc.listenToFlex(stringToPass);
  7. }

FLASH SIDE:
The code in the child swf is not that far off from the code in the parent mxml.
If you recall from above, we passed a reference to the Flex parent into the swf and assigned it as the value of a variable named myFlexParent. So it stands to reason that we need to declare such a variable and that's the first thing that happens in the swf. Next, we do just as we did in the mxml file and set up the conversation functions (talkToFlex and listenToFlex). Since these do pretty much the same thing as their counterparts in the mxml, I'll save you the trouble of reading it twice (and me the trouble of typing it twice). The last thing you see in the code below is just the listener for the button to talk to flex.

Actionscript:
  1. var myFlexParent:Object;
  2.  
  3. function talkToFlex(me:MouseEvent):void{
  4.     myFlexParent.listenToFlash(flashInputTxt.text);
  5. }
  6.  
  7. function listenToFlex(stringToShow:String):void{
  8.     flexSaidTxt.text = stringToShow;
  9. }
  10.  
  11. talkToFlexBtn.addEventListener(MouseEvent.CLICK,talkToFlex);

THE EXAMPLE:

This movie requires Flash Player 9

UP NEXT:
Part 2 of this topic will be very similar to this post except that I will be implementing listeners and a custom event to communicate between Flex and its loaded swf child. I'll try to have that up today or tomorrow.

DOWNLOAD THE ZIP:
Source files here.

http://flash.fincanon.com/wp-content/plugins/sociofluid/images/digg_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/reddit_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/dzone_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/delicious_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/furl_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/technorati_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/google_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/myspace_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/facebook_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://flash.fincanon.com/wp-content/plugins/sociofluid/images/twitter_48.png
Comments (14) Trackbacks (0)
  1. Thank you for posting this. I’ve been looking for a decent tutorial all day.

  2. Wow thanks! I’ve spent half the day going through examples
    that weren’t working. This was the first thing that worked.

  3. No problem guys! I’m glad I could help! :)

  4. Hi guys,

    I have tried your example with simple flash game.

    1. Set the

    function listenToFlex(inc:Number):Void{

    points = inc;

    } in flash

    2. Load that game with swf loader in flex:

    public var mySwfMc:MovieClip;

    private function setSwfMc():void{

    mySwfMc = mySWFLoader.content as MovieClip;

    }

    I create a button to call method from flash:

    private function click():void
    {
    mySwfMc.listenToFlex= 2;
    }

    The game is shown, but when I try to access method listenToFlex, I am getting an error:
    Error #1009: Cannot access a property or method of a null object reference. (mySwfMc is null)

    Any idea?
    Thanx

  5. Problem found.
    I was trying to access property in as2 from as3

  6. Hi oga. I was away from my computer for most of the weekend. Sorry I didn’t get back to you quicker, but glad you found the problem. :)

  7. Thanks for the tutorial! Easy to understand! Thanks!

  8. nice tutorial…….thanks you sir

  9. You’re very welcome and I’m glad to have helped.
    Thanks for reading!

  10. Awesome post – EXACTLY what I was looking for! Great job – thanks!

  11. Hi Julia.
    I’m very glad to hear it helped.
    If you haven’t had a chance yet, check out the part 2 follow up to this post to see if it adds any value to your particular need.

    Thanks for stopping by! :)

  12. Thanks for the easy to understand tut! Adobe needs writers like you!!

  13. I try this example on my PC… but it not working..Can you Please help me why not this working for me?

  14. Can you please specify version , I used Flex3 :)


Leave a comment

(required)

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Trackbacks are disabled.