Advertisement
ActionScript

Quick Tip: How to Debug an AS3 Error #1203

by

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.

Related Posts
  • Code
    Android SDK
    Create a Music Player on Android: User Controls0d63m preview image@2x
    We are building a simple music player app for Android in this series. So far, we have presented a list of the songs on the device and allowed the user to make selections from it, starting playback using the MediaPlayer class in a Service class. In this final part of the series, we will let the user control playback, including skipping to the next and previous tracks, fast-forwarding, rewinding, playing, pausing, and seeking to particular points in the track. We will also display a notification during playback so that the user can jump back to the music player after using other apps.Read More…
  • Game Development
    Implementation
    Write Once, Publish Everywhere With HaxePunk: Making a GamePreviewretinaimage
    You've probably had this experience before: you hear about an awesome game, but then you find out that it's only coming out on the one platform that you don't own. It doesn't have to be this way. In this tutorial, you will learn how to use Haxe to make a game in one development platform that can target multiple gaming platforms, including Linux, Mac, Windows, iOS, Android, and Flash.Read More…
  • Code
    Mobile Development
    C++ Succinctly: TemplatesPreview image@2x
    Template functions and classes serve a similar purpose in C++ as generics serve in C#. They allow you to reuse your code without writing a function or class for each variant you want.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Resources Acquisition Is InitializationPreview image@2x
    This article is all about RAII or Resource Acquisition Is Initialization, a design pattern using C++ code to eliminate resource leaks.Read More…
  • Code
    Mobile Development
    C++ Succinctly: Constructors, Destructors, and OperatorsPreview image@2x
    In this article, we discuss constructors, destructors, and operators in C++. Constructors are a bit more complex in C++ than they are in C#, but, after reading this article, you should have a good understanding of C++ constructors.Read More…
  • Code
    Android SDK
    Android SDK: Java Application ProgrammingAndroid preview@2x
    Our goal in this series is to learn about Android SDK development. So far we explored the development tools, looked at the structure of an Android app project, started to create a user interface, and responded to user interaction. In this tutorial, we will look at the basic structures and concepts in Java that you need to know in order to start developing apps for Android.Read More…