5 Replies Latest reply on Mar 13, 2008 5:03 PM by slaingod

    Flash CS3 to Flex 3 Communication

    nchristiansen
      I apologize, although the posing page says 'Please use "Attach Code" to include any sample code in the message.' The button does not appear in my browser (Firefox).

      I have a UI developed in Flash CS3 by our Graphic Designer. I have code written in Flex that has a bunch of back end computation that needs to be done when one of the UI buttons are clicked.

      The UI.swf file has a handler like this on the button:
      on (release)
      {
      // Gather Info
      // Send info to Flex container.
      if (_root.updatePrefs)
      {
      _root.updatePrefs(the_data);
      }
      }


      The main.mxml file includes:

      <mx:Script>
      <![CDATA[
      public function updatePrefs(the_data:Object):void
      {
      // Do calculations
      }

      [Embed(source="assets/UI.swf")]
      [Bindable]
      public var UI:Class;
      ]]>
      </mx:Script>
      <mx:Image id="background" source="{UI}" x="0" y="0"/>


      It is important to note that the UI.swf file was created with Actionscript 2.0 enabled instead of Actionscript 3.0.

      The call in the onrelease handler seems to not work. What must I do to have the UI.swf file communicate with the Flex file?

      Thanks.
        • 1. Re: Flash CS3 to Flex 3 Communication
          Gregory Lafrance Level 6
          These help links may help:

          Using the LocalConnection class
          Cross-scripting
          • 2. Re: Flash CS3 to Flex 3 Communication
            nchristiansen Level 1
            quote:

            Originally posted by: Greg Lafrance
            These help links may help:

            Using the LocalConnection class
            Cross-scripting


            Apparently the links didn't come through. However, I am looking up both topics in the Flex and Flash documentation.
            • 3. Re: Flash CS3 to Flex 3 Communication
              ntsiii Level 3
              The problem is that an AS2 swf cannot communicate directly with an AS3 swf, they run in different virtual machines.

              The best solution would be to get the loaded swf implemented in AS3.
              Tracy
              • 4. Re: Flash CS3 to Flex 3 Communication
                nchristiansen Level 1
                Both the answers submitted by Greg Lafrance and ntsiii are correct. This message is to summarize their responses and provide the links that did not come though.

                AS 2.0 and AS 3.0 run in different virtual machines so they do not communicate directly.

                You can use the LocalConnection class to communicate between the two virtual machines.

                If you upgrade the CS3 file to use Actionscript 3.0 then you can use the Cross-Scripting method for communication. This is simplified if they are loaded from the same server with the same domain name. They can talk to each other directly.
                • 5. Re: Flash CS3 to Flex 3 Communication
                  slaingod Level 1
                  Using LocalConnection is fairly flaky at best, IMO. Unless you are able to establish some sort of unique ID/connection with each instance of your swf/flex app, then you will have unintended results if there are multiple instances. This means even if someone has your site open on 2 tabs in a browser, then your site becomes unusable if you use LocalConnection. In my experience, you either need to coordinate some sort of unique embed id/LocalConnection name ahead of time so that each instance of your flex app knows which instance of the SWF it needs to talk to, or you need to do some fanciness with SWFLoader to embed the piece directly in the flex app.

                  Attach code is missing for me too, in both FF and IE7....
                  Here is an example of loading a FlexApp inside of another app, which might be adaptable to your situation.


                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" >

                  <mx:Script>
                  <![CDATA[

                  import mx.events.FlexEvent;
                  import mx.managers.SystemManager;
                  import mx.core.Application;

                  // * gets rid of type checking so we can leave strict enabled
                  [Bindable]public var _appLoaded:*;

                  private var load_finish_playlist:int = 0;
                  private var load_finish_name:String = "";

                  private function onCurrentApplicationComplete(oEvent:Event):void
                  {
                  _appLoaded = Application(oEvent.target.application);
                  if(load_finish_playlist > 0) {
                  setPlaylist(load_finish_playlist);
                  }
                  if(load_finish_name != "") {
                  setPlayerName(load_finish_name);
                  }
                  }

                  private function onCompleteAppLoader(oEvent:Event):void
                  {
                  var smAppLoaded:SystemManager = SystemManager(oEvent.target.content); //get a ref to the loaded app
                  //listen for the application.complete event
                  smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onCurrentApplicationComplete);
                  }

                  public function setPlaylist(id:int):void {
                  if(_appLoaded) {
                  _appLoaded.change_playlist(id);
                  } else {
                  load_finish_playlist = id;
                  }
                  }

                  public function setPlayerName(name:String):void {
                  if(_appLoaded) {
                  _appLoaded.set_name(name);
                  } else {
                  load_finish_name = name;
                  }
                  }

                  public function stopPlayback():void {
                  _appLoaded.stop_playback();
                  }

                  public function startPlayback():void {
                  _appLoaded.start_playback();
                  }

                  ]]>
                  </mx:Script>

                  <mx:SWFLoader id="player" x="0" y="0" source="@Embed('../assets/Player.swf')" complete="onCompleteAppLoader(event);" trustContent="true"/>

                  </mx:Canvas>