Copy link to clipboard
Copied
Hello, I have a problem with my ActionScript code. I have an object called carMove which is moved to the right using the arrow key. When this object hits an object called hitObject, I want it to display "Continue". This all works correctly but i'm getting a constant loop message.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HistoryOfSport_fla::MainTimeline/loop()
I think the problem occurs as soon as the carMove hits the object.
Any help please ?
Here is my code -
stage.focus = this;
import flash.events.KeyboardEvent;
import flash.events.Event;
var right:Boolean = false;
Continue.visible = false;
stage.addEventListener( KeyboardEvent.KEY_DOWN, keydown);
stage.addEventListener( KeyboardEvent.KEY_UP, keyup);
stage.addEventListener(Event.ENTER_FRAME, loop);
function keydown(e:KeyboardEvent):void
{
switch ( e.keyCode )
{
case 39 : right = true;{
}
break;
}
if (right) { carMove.x += 20 ; }
carMove.targetsecondObjectMc.rotation += 20;
carMove.targetcrownMc.rotation += 20;
}
if (x> 949.2) {
gotoAndStop(3);
}
function keyup(e:KeyboardEvent):void
{
switch ( e.keyCode )
{
case 39 : right = false;
break;
}
}
function loop (e:Event) :void
{
if ( carMove.hitTestObject( hitObject ) )
{
carMove.visible = false;
Continue.visible = true;
trace(carMove);
}
}
//Adding an event Listener Continue button.
Continue.addEventListener( MouseEvent.MOUSE_UP, ContinueNext );
//Function for when button is pressed.
function ContinueNext( e:MouseEvent ):void
{
//Go to frame 3.
gotoAndStop(3);
}
That error is telling you that one or more of the objects referenced in the function loop isn't available where that function is written. So, if carMove, continue, or hitObject aren't on that frame then that's the origin of the error.
Copy link to clipboard
Copied
That error is telling you that one or more of the objects referenced in the function loop isn't available where that function is written. So, if carMove, continue, or hitObject aren't on that frame then that's the origin of the error.
Copy link to clipboard
Copied
Thank you, managed to find the solution, just need to remove event listeners so they wouldn't carry on to the next frame.
event.currentTarget.removeEventListener(event.type,loop);
event.currentTarget.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now