Friday, September 25, 2009

Helper Classes in ActionScript

I've had a few people ask me recently if you can have multiple classes defined in the same file in ActionScript. This is something that Java developers are probably very familiar with.

Well the answer is yes, you can. They are called Helper Classes, but they are defined a little bit differently from how you might expect, and from how it is done in Java.

Here's a simple example of a helper class:
package helper
{
    public class MainClass
    {
        public function MainClass() {
            var helperCls:HelperClass1 = new HelperClass1();
        }
    }
}
    
import mx.controls.TextInput;

class HelperClass1 extends TextInput 
{
    public function HelperClass1() {
    }
}
Notice that the helper class is defined outside of the package wrapper. And yes, multiple helper classes are allowed in the same file. One thing that surprised me was how the helper class has its own separate section for imports that are also outside of the package block.

Note that helper classes are only available to the main class, and cannot be accessed outside of that file. Even a subclass can't use helper classes belonging to its superclass.

Here is another website which has some more information on this topic:
Class syntax in ActionScript 3.0.

Thursday, September 10, 2009

Image Splitter in AIR


I haven't played with Adobe AIR much, and thought I'd give it a try. I have a whole bunch of sprites - images that contain many different smaller images all side by side - that I wanted to split up into separate images.

So I decided to use Adobe AIR to solve this simple problem. The WindowedApplication lets you choose an input image file and an output directory. The image is loaded into the window and then you can choose the column width and row height for how you want to split the image up. Alternatively you can specify the number of rows and columns. This will draw a gray grid on top of the input image showing you how the image will be split up.

Click on the images on the right side of the page to see the full size image. The top screenshot shows the input image being split into 6 columns and 4 rows. The middle screenshot shows how the background color can be made transparent. And the bottom example shows the output images.

You can also choose between using the JPEGEncoder or the PNGEncoder (PNG supports transparency). If you choose PNG then you can also optionally choose a color that you want to make transparent (e.g. the background color). You can click on the input image to choose the color you want transparent, or you can use the ColorPicker.

There is a filename prefix textbox too for choosing how you want the output files named. After the prefix a number will be appended (e.g. mario1.png, mario2.png) for each output image. Output images will not overwrite existing images unless you check the Overwrite checkbox.

Feel free to modify for you own use. Because it's an AIR application I've only attached screenshots.

- DOWNLOAD SOURCE ZIP FILE -

- VIEW SOURCE -

 
Input Image

Transparent background

Output Images