Another good use of interfaces

Earlier this year, I came to understand how to use interfaces in ActionScript 3. I used them in a particular case where polymorphism was needed but that was it since then. Last week I realized that I could use them in much more case than that. I actually need them in nearly all of my projects.

As I have mentioned in previous post, I always structure the websites I program in the same way. I make one swf file for the preloader, one swf file for the shell (container of navigation that load every sections) and one swf for every section (page) in the site. Sometimes, the preloader needs to inform the shell (main movie) when to start its intro animation. An example of that is the don’t discover Loyalist contest website. When you first load the page, the flash file looks like it is just sitting there waiting for you to click on the start button. But actually it is already preloading the main swf file. That leads to two scenarios; either the user clicks on start and the main movie is not loaded yet so it will start only when it is fully loaded or the user clicks when the main swf is fully loaded and the preloader has to tell the main movie that it can start. I used to disable the strict mode of the compiler in order to call a public method of the main swf document class. That worked fine since I never have so much code involved for a preloader but then I hit a wall.

I came across the same problem when loading external Flash file from which I wanted to use methods. The compiler just didn’t want me to do so. When I started coding in AS3, I, at first, found the compiler to be a bit severe, but I kinda like it now, it finds problems before they arise, so that’s why I don’t want to turn of strict mode for my main movie, where I have thousands of line of code. So I had to find another solution, I tried to import the document class of my external swf with an import statement but the compiler didn’t like it that much because it wasn’t finding classes for Sprites on the stage. That’s when I remembered about interfaces.

Interfaces permit you to ensure that some classes contains some methods. So in my external swf document class I made certain it implemented the interface I was going to us and in my main movie I type casted the loader.content to that interface (Type).

import com.zedia.interfaces.ISection;
var mySection:ISection  = ISection(loader.content);

Now the compiler was happy. I could load external swf and use its methods. One trouble was left; I wasn’t able to add my new content to the display list because my main movie didn’t know that the external swf was extending a DisplayObject. All I had to do to solve this was to type cast my external swf to a Sprite when I added it to the display list like this:

addChild(Sprite(mySection)) ;

That really did the trick and now I am going to use interfaces in most of my projects.

, ,

  1. #1 by dubbeat - April 15th, 2008 at 04:08

    The whole concept can be tricky to own. Real and practical examples like this help the understanding a lot. Thanks for sharing :)

  2. #2 by Nico - December 22nd, 2008 at 14:48

    Thanx man! very helpful

  3. #3 by sideDoor - January 19th, 2009 at 04:44

    Above, when you wrote:

    “So in my external swf document class I made certain it implemented the interface I was going to us and in my main movie I type casted the loader.content to that interface (Type).”

    …did you not mean…

    “…and in my PRELOADER movie I type casted the loader.content to that interface…”, instead of main movie? (Main movie is what you originally declared to be the “shell” movie of your application).

    In this case, I gather that you’re using:

    1. Actions layer of the preloader.swf contains the preloader code and loads the shell movie.
    2. On complete, you run the closing animation of the preloader, and when complete, you call an init() method of the document class from the loaded.
    3. But in order to have access to the document class init() method, you need to access it through type-casting the loader.content property to an interface that ensures the init() method?

    I’m not totally clear here: Is there anyway you can show the classes involved in a simple implementation or a UML diagram or post a zip of the structure?

    Thanks very much! Keep of the great work!

  4. #4 by sideDoor - January 19th, 2009 at 17:04

    I meant: Keep up the great work!

  5. #5 by - January 19th, 2009 at 17:21


    Nah I meant the main movie because the main movie is loading each sections. But in the case of a preloader, your three point would be exact.

    Read this other post for more information about complex preloaders:

  6. #6 by ryan galax - August 21st, 2009 at 16:37

    great stuff here

    thanks a lot !!!

  7. #7 by Hadi - August 26th, 2009 at 13:02

    Thanks for sharing, I’ve been shifted to AS3 for 6 month and up to this moment I was afraid of interfaces but now, I will be using it in my projects, it’s surely great. thank you

  8. #8 by philmill - January 26th, 2010 at 18:26

    I never thought about using an interface this way. I too had issues importing the class of the external swf and trying to cast it without errors. Thank you.

  9. #9 by Håvard Terland - March 12th, 2011 at 10:58

    I was afraid of interfaces, before I found this! Favorited + liked.

  10. #10 by james - July 21st, 2011 at 07:14

    interfaces are great for polymorphism where necessary, but it used to be considered a bad idea, as class abstraction can become a nightmare in terms of tight coupling and dependencies. The really useful thing that I find for them is when defining a contract between you the developer and the developer who implements your code. If you specify an interface then when the developer comes to extend the class that implements that interface they are forced to stick to the implementation details that you have defined in the class. This allows for the class to be extended whilst maintaining the same method signatures so that the compiler can allocate memory without problems in the case of run time type identification etc.

(will not be published)
Subscribe to comments feed

Parse error: syntax error, unexpected ';' in /homepages/25/d169645162/htdocs/wp-content/themes/fusion/footer.php on line 13