Search code examples
actionscript-2flex4dispatchevent

flex 4: swfloader as2 game, can i catch a customevent that was sent using mx.events.EventDispatcher?


I'm building a flex 4 container for action script 2 flash applications. I use <mx:SWFLoader> component to load the game.

I know that i can catch events or even custom events from an action script 3 application.

working example for action script 3 (not 2):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" 
           minHeight="600" creationComplete="init()">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        import Red5Event;


        private function handleRed5Event(e:Red5Event):void {
            Alert.show("yay");
        }

        private function init():void {

               this.fileSwf.content.addEventListener(Red5Event.CONTROL_TYPE
                                                     ,handleRed5Event);
        }

    ]]>
</fx:Script>
    <mx:SWFLoader id="fileSwf" source="file.swf" />
</s:Application>

then in the flash application i extend the event class, adding the proper control type and setting bubbles to true, so whenever i dispatch an event, it's probably being catched by the flex application.

I understood that using as2 i can dispatch custom events using the following example:

import mx.events.EventDispatcher;

class Sender {

   // these three lines are needed to use EventDispatcher
   public var addEventListener:Function;
   public var removeEventListener:Function;
   public var dispatchEvent:Function;   


   public function Sender() {
      // this line must be in the constructor of the class
      EventDispatcher.initialize(this);

      // dispatch an event once per second

   }

   public function sendEvent():Void {
      dispatchEvent({type:"xpoControl"});       
      trace("event sent!");
   }
}   

can i somehow dispatch an event in action script 2 flash application that the flex 4 container will be able to catch?

thanks!


Solution

  • after a lot of reading.. what i request may not be possible.

    Actually, there are work-arounds. You are correct in that the way you're trying to accomplish this isn't really possible because of security restrictions. However, you could build what I like to call a marshaller-adapter via the LocalConnection class. You will need to have a method in AS2 that uses a localConnection to communicate with the flex 4 side. You will have to use simple types and pass the properties of your Red5Event more generically, but you should be able to accomplish what you need with your custom 'marshaller-adapter'.

    Best of luck, Jeremy