How Can I Solve This Error in Flash Game

Let me walk you through a problem I faced while building a Flash puzzle game and more importantly, how I solved it. If you’re working with ActionScript 3 and have ever seen a TypeError: Error #1009 or Error #2007, this post is for you.

This all started when I tried to move my game from Frame 1 to Frame 5 on the timeline. Everything broke. Suddenly, my drag-and-drop pieces stopped working, and the errors started pouring in.

The Problem

So here’s the setup: I was creating a simple drag-and-drop puzzle game. I had a DragDrop class handling the pieces and a Map class managing the stage logic. Everything worked when the game was loaded on Frame 1.

But when I placed the game on Frame 5, I started seeing these nasty errors:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
TypeError: Error #2007: Parameter hitTestObject must be non-null.

Error Breakdown

These errors were confusing at first, so I took a closer look.

Error #1009

This error means I was trying to access a property or call a method on something that was null. Here’s the guilty line:

if (hitTestObject(target))

Turns out, if target is null, hitTestObject() throws this error.

Error #2007

This one was a follow-up disaster. hitTestObject() needs a non-null argument. If target hasn’t been defined yet, it triggers this error.

Why This Happen

This bug comes from how Flash handles timeline frames. Code runs even if the display objects you’re referencing haven’t been instantiated yet which is exactly what happened in my case.

In my Map class, I had this:

currentObject.target = getChildByName(currentObject.name + "_target");

The problem tt1_target didn’t exist at the time this code ran because it wasn’t loaded until Frame 5.

The Solution Delay Initialization

I needed a way to tell Flash, “Hey, wait until everything’s on stage before running this logic.”

The answer was to use Event.ADDED_TO_STAGE. This event only fires when the object is fully added to the stage so it’s safe to reference display objects at that point.

Final Working Code

Here’s how I fixed both files:

Map.as

package {
import flash.display.*;
import flash.events.*;

public class Map extends MovieClip {
var dragdrops:Array;

public function Map() {
// Delay initialization
addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
dragdrops = [tt1];
var currentObject:DragDrop;
for (var i:uint = 0; i < dragdrops.length; i++) {
currentObject = dragdrops[i];
currentObject.target = getChildByName(currentObject.name + "_target");

if (currentObject.target == null) {
trace("Warning: Target not found for " + currentObject.name);
}
}
}

public function match():void {
trace("Matched!");
}
}
}

DragDrop.as

package {
import flash.display.*;
import flash.events.*;

public class DragDrop extends Sprite {
public var target:DisplayObject;
var origX:Number;
var origY:Number;

public function DragDrop() {
addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
origX = x;
origY = y;
addEventListener(MouseEvent.MOUSE_DOWN, drag);
buttonMode = true;
}

function drag(evt:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
startDrag();
parent.addChild(this);
}

function drop(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
stopDrag();

if (target && hitTestObject(target)) {
visible = false;
target.alpha = 1;
Object(parent).match();
}

x = origX;
y = origY;
}
}
}

Additional Feature I Added

Once the game was fixed, I decided to level it up with a few extra features:

Snap to Target

Make the dragged object snap into place:

if (target && hitTestObject(target)) {
x = target.x;
y = target.y;
visible = false;
target.alpha = 1;
Object(parent).match();
}

Play Sound on Match

Add a sound effect when a player makes a correct match:

import flash.media.Sound;
var snd:Sound = new CorrectSound(); // Add to library
snd.play();

Score Counter

Track how many pieces were correctly placed:

// In Map.as
public var score:int = 0;

public function match():void {
score++;
trace("Score: " + score);
}

Highlight Targets on Hover

Use GlowFilter to visually highlight targets when a piece is nearby.

Final Thought

This whole bug journey taught me valuable lessons about Flash’s lifecycle especially how display objects behave across timeline frames. I learned that just because code compiles doesn’t mean all your objects are actually present when the code runs. By using Event.ADDED_TO_STAGE for safe initialization and guarding against null references, I avoided hours of frustrating errors. If you’re working with Flash games, always make sure your objects are initialized, use event-based timing, and never assume methods like hitTestObject are safe.

Related blog posts