Monday, May 12, 2008

MovieClip Subclass without Asset

Nearly every example I see creating MovieClip subclasses involve an asset -- bitmap, video, or sound. What if a class draws itself? A few tricks are required for these classes to work correctly.

Building on the earlier demo, add a subdirectory under src called oas2 (for Open ActionScript 2.0). Right click on the src folder: New -> Other... Then: General -> Folder. When the dialog appears, enter oas2 in the Folder name box. Click Finish.

Add a new ActionScript Class in the oas2 subfolder called Rectangle. Copy and paste this code. Save changes.

Open the main class file, Application.as. Two modifications are required.
  1. Above the class declaration:
    import oas2.Rectangle ;
  2. At the bottom of the class constructor:
    var my_rectangle:Rectangle = Rectangle.create(this, "my_rect", 50, 50, 25, 25) ;
Save the file, and the SWF file is automatically rebuilt by Eclipse. Click to see a grey rectangle in the top left of the Flash demo.

No comments: