30
May
08

Linkage Classes as Arguments

I like to make my custom UI elements easy to skin. If we wanted to create a check box class that was easy to skin, one thing we could do is create an FLA based AS3 component. Unfortunately to do that takes advanced programming skills. If you're an intermediate programmer you might want to be able to create a skinnable UI element without all of the AS3 component architecture.

One way to achieve this is by creating a custom class that uses Library elements as assets. To increase flexibility (for swapping skins, for example), you can create each graphical element of your check box, store them in your Library, assign a linkage class name to each element, and then pass those linkage class names into your custom class.

In the above library you'll see that the movie clips GreenBg and YellowCheck have associated linkage classes of the same names. Wouldn't it be nice to simply pass the names of each linkage class into the constructor of a check box class? The SWF below demonstrates this idea:

This movie requires Flash Player 9. Please update your player.

Download: Skinnable Check Box  Download Skinnable Check Box (9.2 KB, 2,092 hits)

The checkbox in the above SWF is created with the following code:

ActionScript 3.0:
  1. var cb:SkinnableCheckBox = new SkinnableCheckBox(GreenBg, YellowCheck);
  2. cb.x = 465;
  3. cb.y = 120;
  4. addChild(cb);

The GreenBg and YellowCheck linkage classes are passed into our custom class called SkinnableCheckBox. SkinnableCheckBox is a flexible class with the ability to create new instances of any linkage class passed in to its constructor. Let's take a look at the inner workings of SkinnableCheckBox:

ActionScript 3.0:
  1. package {
  2.  
  3.    import flash.display.*;
  4.    import flash.events.MouseEvent;
  5.    
  6.    public class SkinnableCheckBox extends Sprite {
  7.      
  8.       private var BgSkin:Class;
  9.       private var CheckSkin:Class;
  10.      
  11.       private var _bg:MovieClip;
  12.       private var _check:MovieClip;
  13.      
  14.       public function SkinnableCheckBox(bgSkin:Class, checkSkin:Class) {
  15.          BgSkin = bgSkin;
  16.          CheckSkin = checkSkin;
  17.          build();
  18.          buttonMode = true;
  19.          addEventListener(MouseEvent.CLICK, onToggle, false, 0, true);
  20.       }
  21.      
  22.       private function build():void {
  23.          _bg = new BgSkin();
  24.          _check = new CheckSkin();
  25.          addChild(_bg);
  26.          addChild(_check);
  27.       }
  28.      
  29.       private function onToggle(evt:MouseEvent):void {
  30.          _check.visible = !_check.visible;
  31.       }
  32.    }
  33. }

Lines 8 and 9 create private properties used for storing references to the linkage classes we want to instantiate. These classes come in to the constructor through arguments seen in line 13 and are assigned to class properties in lines 15 and 16. The build() method on line 17 is responsible for creating the new instances and adding them to the display list. SkinnableCheckBox extends Sprite and is, itself, added to the display list (as seen in line 4 of the previous code block). So, using addChild() for the skin parts is akin to adding children to a parent Sprite. It's important to note that this class is not meant to be a fully functional check box class. It's just meant to illustrate a possible skinning method.

Here is an example of this same class being used with a few different skins:

This movie requires Flash Player 9. Please update your player.

Click to toggle check boxes

Download: Skinnable Check Box  Download Skinnable Check Box (9.2 KB, 2,092 hits)

We can easily use the same custom class with numerous skins:

ActionScript 3.0:
  1. var cb0:SkinnableCheckBox = new SkinnableCheckBox(BlueBg,
  2.                                       LightBlueCheck);
  3. cb0.x = 50;
  4. cb0.y = 50;
  5. addChild(cb0);
  6.  
  7. var cb1:SkinnableCheckBox = new SkinnableCheckBox(FlatGreyBg,
  8.                                       FlatWhiteCheck);
  9. cb1.x = 100;
  10. cb1.y = 50;
  11. addChild(cb1);
  12.  
  13. var cb2:SkinnableCheckBox = new SkinnableCheckBox(BigBg, BigCheck);
  14. cb2.x = 150;
  15. cb2.y = 40;
  16. addChild(cb2);
  17.  
  18. var cb3:SkinnableCheckBox = new SkinnableCheckBox(SketchBg, SketchCheck);
  19. cb3.x = 220;
  20. cb3.y = 45;
  21. addChild(cb3);

In upcoming posts I'll be using this technique to build different UI classes. So, be sure to check back again.

Print This Post Print This Post


12 Responses to “Linkage Classes as Arguments”


  1. 1 Jake Jun 5th, 2008 at 1:18 am

    Awesome quick tutorial

  2. 2 Zevan Jun 5th, 2008 at 10:25 pm

    Thanks Jake. Glad you liked it.

  3. 3 Cor Jun 21st, 2008 at 3:20 pm

    Very nice, Zevan!
    Looking forward to your upcoming post...
    Or is it already somewhere?

  4. 4 kumnanan Jun 24th, 2008 at 4:42 am

    Thx dude...!

  5. 5 Evan Jun 24th, 2008 at 11:02 pm

    Great tutorial and I really enjoy the brilliant yet understandable way all the tutorials and the wonderful book explain Actionscript 3.0. thanks so much!

  6. 6 Zevan Jun 24th, 2008 at 11:33 pm

    thanks. Glad you guys liked the tutorial. Definitely a good deal more to come on this subject.

    best,

    z

  7. 7 Ervin Jul 11th, 2008 at 10:12 am

    It's Good Tutorial!Thank..

  8. 8 rezki Jul 29th, 2008 at 1:37 pm

    im newbie. thats really quick and understandable, thanx i like it.

  9. 9 Anonymous Sep 18th, 2008 at 9:45 am

    This is actually a great example of class encapsulation, a concept that is difficult to maintain throughout Flash/AS Development.

  10. 10 Eli Nov 2nd, 2008 at 6:09 am

    I would like to add to the others comments and to whom who read your book. It's really enjoyable and it covers most of the topics you will encounter in your everyday ActionScript 3 development. I think brilliant is a good way of describing it.

  11. 11 fjo Jun 25th, 2009 at 11:44 pm

    Hi, this might be a bit off the topic.... I'm getting this weird error every time I try to use custom class:
    Error: Error #2136: The SWF file file:///Cietais/Users/andrisfeodorovs/Documents/_Flash/AS3_maacos/_Learning AS 3.0/06_Classes/inheritance/90 Custom Class.swf contains invalid data.
    at Main/::frame1()
    Any thoughts - I have done everything according to the book. I downloaded example files on OOP chapter 6 and none of them worked too.... any ideas? I have Flash CS3 - could it be Flash bug or smthing like that?
    I've done everything according to the book but still nothing works.......

    Thnks...

  12. 12 Hardik Sep 14th, 2010 at 4:28 pm

    how u included SkinnableCheckBox.as file in fla file.

Leave a Reply




Speaking



Archives