Search code examples
actionscript-3flash-cs5hittest

How can I solve this error in Flash game?


I have a problem in Flash puzzle game. If I create the game in the first frame of my timeline it's working, but if the game has been created (for example) in 5th frame it does'nt work!

It send me this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Map() TypeError: Error #2007: Parameter hitTestObject must be non-null. at flash.display::DisplayObject/_hitTest() at flash.display::DisplayObject/hitTestObject() at DragDrop/drop()

dragdrop class

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


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


        public function DragDrop()
        {
            // constructor code
            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(hitTestObject(target))
            {
                visible = false;
                target.alpha = 1;
                Object(parent).match();
            }

            x = origX;
            y = origY;
        }


    }
}

I think the problem is in var target! and I don't know how to solve it.

Map.as enter code here package {

import flash.display.*;
import flash.events.*;


public class Map extends MovieClip
{
    var dragdrops:Array;


    public function Map()
    {
        // constructor code
        dragdrops = [tt1];
        var currentObject:DragDrop;
        for(var i:uint = 0; i < dragdrops.length; i++)
        {
            currentObject = dragdrops[i];
            currentObject.target = getChildByName(currentObject.name + "_target");
        }
    }

    public function match():void
    {

    }
}

}


Solution

  • Edit:

    There are multiple problems with the code. Too many to list, I'm afraid, but the biggest one is:

    You're declaring a map, and trying to add your object to it, before your object exists. It doesn't exist until frame 5, so this won't work. I've re-written the code below, but honestly, there is so much wrong with the code that it's just not possible to fix without re-writing significant portions of it.

    package 
    {
    
    import flash.display.*;
    import flash.events.*;
    
    
    public class Map extends MovieClip
    {
        var dragdrops:Array;
    
    
        public function Map()
        {
            // constructor code
            dragdrops = new Array();
    
        }
        public function addElement(gamepiece:DragDrop):void {
            dragdrops.push(gamepiece);
        }
        public function addChildElements():void {
            var currentObject:Object;
            for(var i:uint = 0; i < dragdrops.length; i++)
            {
                currentObject = dragdrops[i];
                currentObject.test();
                currentObject.target = (currentObject.name + "_target"); // this should work now, but doesn't. Why? 
                currentObject.target.test();
            }
        }
        public function match():void
        {
    
        }
    }
    
    }
    

    Then, on frame one, I added:

    var map:Map = new Map();
    

    Then, on frame five, I added:

    map.addElement(tt1);
    map.addChildElements();
    

    This got tt1 added to map, at least, but that's as far as I got. Your problem now is;

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

    It's the correct name, now, but it won't add it to target. That's as much as I can do.