Using SWF files that implement a Specific Interface

How about modularizing your application so that other developers can develop external modules by only knowing a specific set of interfaces. Based on this idea I built a little test to see if it’s possible to create SWF files which main class implements a specific Interface, then loading them into another SWF and cast them to the Interface specified, and I found out that it works!

here is the test:

First of all I’ve created my Interface:
MyInterface.as

package
{
    public interface MyInterface
    {
        function getValue():String;
        function setValue(value:String):void;
        function getRandomInt():int;
    }
}

I’ve created a swf file that implements the interface above
SWFImplementingMyInterface.as

package
{
    import flash.display.Sprite;

    public class SWFImplementingMyInterface extends Sprite implements MyInterface
    {
        private var myValue:String;

        public function MyExternalClass()
        {
        }

        public function getValue():String
        {
            return myValue;
        }

        public function setValue(value:String):void
        {
            myValue = value;
        }

        public function getRandomInt():int
        {
            return 125;
        }
    }
}

And finally another SWF that loads the previous SWF, casts it to MyInterface and use MyInterface’s methods.

package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    public class Main extends Sprite
    {
        private var loader:Loader;
        private var myVar:MyInterface;
        public function Main()
        {
            loader = new Loader();
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(event:Event):void
        {
            loader = new Loader();

            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onExternalSWFLoaded);
            loader.load(new URLRequest("MyExternalClass.swf"));
        }

        private function onExternalSWFLoaded(event:Event):void
        {
            try{
                myVar = MyInterface(loader.content);
                trace("random int = "+myVar.getRandomInt());
                myVar.setValue("a value");
                trace("value = "+myVar.getValue());
            }catch(castingError:TypeError)
            {
                trace("Error : "+castingError.message);
            }
        }
    }
}

It sounds like something obvious but with Adobe sometimes you cannot assume that everything works as expected :)

zip-icon.thumbnail

download all the files above.

This entry was posted in AS3, components, Flash, Thoughts. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • http://alecmce.com/as3/interfacing-swf-files Interfacing SWF Files at AlecMcE.com
  • http://www.visible-form.com/blog Rich Rodecker

    Yep, I do that all the time, for modules as well as font swfs for runtime font loading.

  • http://www.visible-form.com/blog Rich Rodecker

    Yep, I do that all the time, for modules as well as font swfs for runtime font loading.

  • Anonymous

    Hi Rich,
    thanks for leaving a comment. I’m curious to know what you mean with “font swfs for runtime font loading” could you explain?

  • vizio

    Hi Rich,
    thanks for leaving a comment. I’m curious to know what you mean with “font swfs for runtime font loading” could you explain?

  • http://blog.vizio360.co.uk/2009/12/unload-issue-with-external-swf-files-that-implement-an-interface/ vizio 360º » Unload issue with external SWF files that implement an interface

    [...] To achive that I implement a common interface in all my SWF modules.  So far so good as it works perfectly as seen on my previous blog Using SWF files that implement a specific interface. [...]

  • Anonymous

    Hi Rman,

    could you explain a bit more the problem you are having? 

blog comments powered by Disqus