1

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
    {

    }
}

}

arshia
  • 13
  • 4

2 Answers2

0

It's because your hitTestObject method isn't correctly invoked. This method must be invoked in a Display Object instance to test if another instance of a Display Object hits it:

if (myDisplayObject.hitTestObject(anotherDisplayObject))
{
    // do stuff
}

Adobe help about hitTestObject method.

Edit

So you should write you class like that:

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

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

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

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

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

        private function drop(evt:MouseEvent):void
        {  
            target = (evt.target as DisplayObject);
            stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
            stopDrag();

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

            x = origX;
            y = origY;
        }
    }
}

Remark

You shouldn't call your variable target, because its the name of a Flash native variable. Rename it targ for example.

helloflash
  • 2,457
  • 2
  • 15
  • 19
  • hi tanx very much , i'm sorry but i'm dont undrestand quite , would you mind you change my own code that i undrestand what shoud i do ? – arshia Jan 02 '15 at 14:20
  • tank you so much i copy your code and run the game and when i doing drag and drop my object in the game , The desired object is hidden send new error : TypeError: Error #1009: Cannot access a property or method of a null object reference. at Map() :( can you see a Map.as fail ? :(( – arshia Jan 02 '15 at 16:47
0

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.

Martyn Shutt
  • 1,671
  • 18
  • 25
  • tnx i add this line = this.target = (evt.target as DisplayObject); but now send me this error : TypeError: Error #1009: Cannot access a property or method of a null object reference. at Map() and when i doing drag and drop my object in the game , The desired object is hidden ! :(( please download my game and chek it : https://www.mediafire.com/?u0y39kll1yo5tib – arshia Jan 02 '15 at 15:56
  • i think we must change the map.as !؟ – arshia Jan 02 '15 at 15:58
  • Ah. I see what it is you're doing. Your Map is setting the target in DragDrop. In that case this isn't your problem, and I would suggest going back to what you were working with originally as this isn't your problem. I can't open your fla as I use CS5. Where you are creating the objects from these classes? Do you have a Document Class set up, or are you declaring these objects in the first frame of your fla? It would help if you posted that code too, and where you have that code in your fla. – Martyn Shutt Jan 02 '15 at 16:19
  • Really thanks for your help ♥ i saved fla to cs5 and now you can chek it please :( https://www.mediafire.com/?b0ql0ncd4nl6bim – arshia Jan 02 '15 at 16:39
  • I've made this game as Lynda training :(( – arshia Jan 02 '15 at 16:40
  • 1
    I'm afraid it's unworkable. I can't make heads of tails of it. There are so many things wrong with the code that it would need to be completely re-written. I get one bit to work, and then there's another problem. I'm going to list some of them in my answer. If you have followed the tutorial step by step, I would strongly suggest avoiding continuing further, because it's half-implemented. – Martyn Shutt Jan 02 '15 at 19:01
  • @MartynShutt I agree with you: the code would need to be completely re-written. Asking a question about this code is something like a nonsense. – helloflash Jan 02 '15 at 19:50
  • @arshia You should re-write your classes step by step, logically. – helloflash Jan 02 '15 at 19:51
  • :((( i'm test your code and run the game and no error but in game when doing drag and drop this abject hidden :((( and not work :((( if you can download this link https://www.mediafire.com/?lg5saz6whfbxbh7 it's another game and it's work but just frame 1 :((( – arshia Jan 02 '15 at 20:13
  • Have you extended this game beyond the tutorial? It seems like it's all designed to work in a single frame. Working with code across multiple frames in Flash is a nightmare. You would be better off learning to do it all in code. Tutorials like this are very rigid and specific. Learning to write games purely in ActionScript 3.0 using Flash Builder or FlashDevelop is the best way to learn how to make games. I just can't get your game to work. Sorry. – Martyn Shutt Jan 02 '15 at 20:41