IDisplayObject? – Getting around the lack of an interface for the DisplayObject in Flash

by

Interfaces are amazing things. When I was young and green, I didn’t understand their purpose, but after working with them for a while, I will defend their use to the end. I try to create lots of interfaces early on in my projects and I find that by keeping things flexible, it saves more time despite the extra typing, pardon the double entendre.

But this is not an article about why interfaces are so great. No, this is an article about the mysterious gap in the Flash Player API with regards to an interface for DisplayObjects. If you have a class typed as something like IView, there’s no way to enure that IView can be a parameter of display list functions like addChild().

Why can’t we guarantee that something is a DisplayObject through interfaces? There is a practical answer. The inner workings of the Flash Player are slowing unfolding to us with each version. We now have low level access to sound, byte streams, and text rendering. However, the display list still relies exclusively on one of the only truly abstract classes in ActionScript 3.0, the DisplayObject. Because a low-level drawing engine is not exposed to AS3 programmers, we don’t know how it works and we cannot guarantee through an interface that an arbitrary class can be drawn.

Fortunately, there are some workarounds that can help, each with their own strengths and weaknesses. Personally, I think #2 is the way to go. It’s practically dripping with polymorphic goodness.

  1. Make your interface inherit from IBitmapDrawable and IEventDispatcher

    DisplayObject is the only class that uses both of these two interfaces so by combining them in your interface, you can sort of be sure you’re working with a DisplayObject. Still, this is my least favorite solution

    Pros:

    • Uses interfaces exclusively.
    • Only a DisplayObject would implement both of these.

    Cons:

    • There’s nothing to stop you from using this to create a class that isn’t a DisplayObject and that could cause problems.
    • You would still have to type cast to DisplayObject to use this as an argument for addChild().
  2. Create an interface that exposes a method to return the object as a DisplayObject

    My personal favorite solution. It not only solves the problem of guaranteeing that a class can go in the display list, it doesn’t even require that the class be a DisplayObject.

    Here’s an example of what this would look like. IDisplayObject.as

    Pros:

    • Flexible solution that conforms to good OOD principles.
    • Uses interfaces exclusively.
    • Easy to apply. You can usually implement it with a single line of code: return this;

    Cons:

    • Requires you to use asDisplayObject() every time you want to use the object as a DisplayObject.
    • Calling foo.asDisplayObject() would throw an exception if foo was null so additional checks may be necessary.
  3. Use an abstract class that inherits from DisplayObject

    Not an ideal solution primarily because Flash doesn’t have true abstract classes (although there are workarounds) but still totally valid.

    Pros:

    • Guarantees that the object is a DisplayObject. No type casting required.
    • Abstract classes can be treated virtually the same as an interface for practical use.

    Cons:

    • No true abstract classes in Flash.
    • Not as flexible as an interface-based solution since it forces you to inherit from DisplayObject.
Tagged , , , , ,
  • http://pedram.mp Pedram

    thanks buddy,
    I think IBitmapDrawable solution isn’t bad.
    good luck…

  • http://www.jhead.net jHead

    Hi, great post.
    A friend and colleague of mine posted a couple weeks ago a feature request on the Adobe Jira about the lack of an interface for the DisplayObject.
    You can vote for it and help to improve AS3 ;)
    Here is the link : http://bugs.adobe.com/jira/browse/ASL-45

  • http://www.imigin-labs.com/ Stat

    If you are familiar with the Flex SDK, you can inspect mx.core::IDisplayObjectInterface.as. You are essentially on the right track with regards to #2 however, instead of stubbing a method to return a DisplayObject, you force the Object which implements the interface to include the methods listed in the previous stated .as file. By doing so, you can avoid having to typecast the implementing object to a DisplayObject while calling addChild/removeChild. Simply including the contents of that as file will guarantee the object performs as a DisplayObject.

    Not sure if that was what you were going for, but I have found it very useful for maintaining interfaces which are regarded as DisplayObject types.

    Chris

  • Pingback: Weekly Shared Items – 16. March, 2010 | TOXIN LABS - weblog of a german design student from wuerzburg

  • http://lukebayes.com Luke Bayes

    If you’re rolling your own concepts like this, drop inheritance in favor of composition.

    We use interfaces like, Layoutable, Styleable, Composable, etc. And our entities compose Views which are where we hide the Flash DisplayList.

  • http://ilumbo.org/ Pimm Hogeling

    Nice article. Common problem.

    I always use the second solution, specifying a method that returns a display object.

    haXe allows you to specify properties in interfaces, too. Whenever I do haXe, I use a property instead of a method. Requires just a bit less boilerplate code and feels slightly more intuitive.

    And yes, what Luke said.

  • Pingback: Using Interfaces with Display List Objects in AS3.0 « timshaya