0

so I'm still fairly new to AS3 and I'm getting my head around the errors and stuff and I'm trying to figure out why this is not working how it should, the function still executes but it leaves an error on my console. Here is my code:

import flash.events.MouseEvent;
import flash.display.MovieClip;

stop();

var activeHitArray:Array = new Array(word_select_box);
var activeDropArray:Array = new Array(answer1_word, answer2_word, answer3_word);
var hitPositionsArray: Array = new Array();

for (var numi:int = 0; numi < activeDropArray.length; numi++) {
activeDropArray[numi].buttonMode = true;
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
activeDropArray[numi].addEventListener(MouseEvent.MOUSE_UP, mouseup1);

     hitPositionsArray.push({xPos:activeDropArray[numi].x,        yPos:activeDropArray[numi].y});

}

function mousedown1(event:MouseEvent):void {
event.currentTarget.startDrag();
setChildIndex(MovieClip(event.currentTarget), numChildren - 1);
}

function mouseup1(event:MouseEvent):void {
var dropindex1:int = activeDropArray.indexOf(event.currentTarget);
var target:MovieClip = event.currentTarget as MovieClip;
target.stopDrag();

if(target.hitTestObject(activeDropArray[dropindex1])){
           // target.x = activeHitArray[dropindex1].x;
            //target.y = activeHitArray[dropindex1].y;

            if(answer1_word.hitTestObject(word_select_box)){
                   gotoAndStop("6");
            }

} else {
        target.x = hitPositionsArray[dropindex1].xPos;
        target.y = hitPositionsArray[dropindex1].yPos;
}


}

And the Error I'm getting through is:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
  at game_flv_fla::MainTimeline/frame6()
  at flash.display::MovieClip/gotoAndStop()
  at game_flv_fla::MainTimeline/mouseup1()

The only thing I can think of is that it is something to do with the gotoAndPlay() because when I place a trace into it's place I get no errors.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Aiden
  • 53
  • 2
  • 11
  • Look at the first comment on [this question](http://stackoverflow.com/questions/16027616/as3-flash-error-cannot-access-a-property-or-method-of-a-null-object-reference#comment22866156_16027616). Same applies here. – Marty Apr 17 '13 at 12:22

1 Answers1

0

I copied your code, compiled and lauched it in Flash IDE. It works! :) There were no errors.

But I know where the isue may lay. The addEventListeners are still on, no matter if there still are the objects they were linked to. You need to clean all active things before going to next frame:

if(target.hitTestObject(activeDropArray[dropindex1])){
    if(answer1_word.hitTestObject(word_select_box)){
        for (var i:uint = 0; i < activeDropArray.length; i++) {
            activeDropArray[i].removeEventListener(MouseEvent.MOUSE_DOWN, mousedown1);
            activeDropArray[i].removeEventListener(MouseEvent.MOUSE_UP, mouseup1);
        }
        gotoAndStop("6");
    }
}
SzRaPnEL
  • 171
  • 3
  • 17
  • Hey man!, just tried working from the code you give me there and there is apparenly a syntax error and I can't work out which bit D: , I'm hopeless at actionscript, sorry man D: – Aiden Apr 17 '13 at 14:29
  • The only thing I have added, is the 'for' loop inside your hit test condition. And it is only removing eventListeners from objects because you don't need them anymore. I copied your code and it didn't generate any error. – SzRaPnEL Apr 17 '13 at 20:58
  • That's very odd man, when I implement the code that you wrote (and I typed it and C&P) I get a syntax error, I am very confused D: – Aiden Apr 17 '13 at 21:35