Advertisement

Quick Tip: How to Debug an AS3 Error #1203

by
This post is part of a series called How to Fix Bugs in Flash.
Quick Tip: How to Debug an AS3 Error #1120

In this Quick Tip, we'll discuss the confusing AS3 error 1203, "no constructor found", which crops up without provocation and is rather unfortunately worded. We'll examine the scenario in which it will appear, and how to fix it.


Step 1: An Obligatory But Somewhat Useless Description of Error 1203

First, here is the language of the error:

1203: No default constructor found in base class flash.display:BitmapData.

It's difficult to break this down into plain English. So let's next move on to a scenario that produces this error.


Step 2: A Busted Project

You can find this project in the download package, in the folder "busted". It consists of the following (which you can use to re-create this project if you prefer a hands-on approach):

  1. A FLA named Busted.fla. It is empty, but is linked to the document class described next.
  2. A document class named Busted.as. This extends Sprite and simply creates an instance of the class described next, and adds it to the stage.
    The contents of the class are as follows:

        package {
            import flash.display.*;
            public class Busted extends Sprite {
                public function Busted() {
                    var bmp:Bitmap = new Bitmap(new BustedBitmap());
                    addChild(bmp);
                }
            }
        }

    The BustedBitmap class extends BitmapData, and so we use it to feed a Bitmap object, which is then added to the stage.

  3. Another class file named BustedBitmap.as. As mentioned previously, this extends BitmapData, and its purpose is to be a BitmapData that automatically supplies itself with some Perlin noise. Its contents are:

        package {
            import flash.display.*;
            public class BustedBitmap extends BitmapData {
                public function BustedBitmap() {
                    perlinNoise(100, 100, 2, 1024, false, false, 7, true);
                }
            }
        }

    So, in the BustedBitmap constructor, we call perlinNoise() and thus we automatically have something interesting in the pixel data, so we can see something right away when displaying on the stage.

Now, go ahead and test the movie. You will get Error 1203, pointed at line 4 of BustedBitmap.as. And line 4 is...


public function BustedBitmap() {

Step 3: What's All This, Then?

So what happened? The big thing to keep in mind is that we're writing a class that is a subclass of another class (BitmapData in this case). Also keep in mind that when a class is instantiated, the constructor is called as part of the "new" process.

If you do not actually write a constructor, ActionScript will imply one for you. It will look like this:

public function [NameOfClass]() {

}

Where "[NameOfClass]" will match the name of your class.

Similarly, if you write a subclass, and don't call super() in it, then ActionScript will imply that call for you, as well. It will look like this:

public function [NameOfSubclass]() {
    super();
    // Other explicit code in subclass constructor
}

Note that it happens on the first line, and is simply a call to super with no arguments.

Finally, we get to the root of the problem. If your subclass omits an explicit call to super, and your superclass's constructor has required parameters, then your implicit call to super does not supply the required parameters. You'd think that an argument mismatch error could catch this, but consider that since there isn't any code actually written to make the mismatch, ActionScript can only complain that the lack of a default constructor.

In our example, BustedBitmap extend BitmapData, which has a constructor defined in the documentation as so:

BitmapData(width:int, height:int, transparent:Boolean = true, fillColor:uint = 0xFFFFFFFF)

The first two parameters are required, and we're not supplying them.


Step 4: It's as Simple As...

The solution is, once you understand the problem, extremely simple. You need to explicitly call the super constructor, at least if there are required parameters. In other words, in our BustedBitmap class, this following highlighted line will fix the problem:

package {
    import flash.display.*;
    public class BustedBitmap extends BitmapData {
        public function BustedBitmap() {
            super(600, 400);
            perlinNoise(100, 100, 2, 1024, false, false, 7, true);
        }
    }
}

And that will do it.

As a teachable moment, I'd like to add that, when extending another class, it's generally a good idea to include the explicit call to super. Even if there are no parameters at all, required or otherwise (as in Sprite or MovieClip), an explicit call can declare intention and awareness of the nature of the subclass. More practically, explicit calls give you an opportunity to run some code prior to the call to super, which may be helpful in certain situations.


Step 5: ...And That Is All

Yet another error demystified; the challenge for this one is that the real cause of the error is quite buried. Once you become handy with a shovel, though, the fix is easy enough.

Thanks for reading, and be ready for more debugging tips.

Advertisement