0
votes

I keep getting the #1009 error coming up, I checked that everything was on my stage and was named correctly.

This is the error message that pops up:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Videogame_fla::MainTimeline/loop()

Here is my code:

var carrot:Number = Math.floor(Math.random() * 460) +0;
var carrot1:Number = Math.floor(Math.random() * 460) +0;
var carrot2:Number = Math.floor(Math.random() * 460)+0;
var carrot3:Number = Math.floor(Math.random() * 460)+0;
var carrot4:Number = Math.floor(Math.random() * 460)+0;

var bomb:Number = Math.floor(Math.random() * 460)+0;
var bomb1:Number = Math.floor(Math.random() * 460)+0;
var bomb2:Number = Math.floor(Math.random() * 460)+0;
var bomb3:Number = Math.floor(Math.random() * 460)+0;
var bomb4:Number = Math.floor(Math.random() * 460)+0;

var cloud1:Number = Math.floor(Math.random() * 460)+0;
var cloud2:Number = Math.floor(Math.random() * 460)+0;
var cloud3:Number = Math.floor(Math.random() * 460)+0;
var cloud4:Number = Math.floor(Math.random() * 460)+0;


stage.addEventListener(Event.ENTER_FRAME,loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownevent);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpevent);


 function loop(myevent:Event)
{

carrot_mc.x = carrot;
carrot1_mc.x = carrot1;
    carrot2_mc.x = carrot2;
carrot3_mc.x = carrot3;
carrot4_mc.x = carrot4;

bomb_mc.x = bomb;
bomb1_mc.x = bomb1;
bomb2_mc.x = bomb2;
bomb3_mc.x = bomb3;
bomb4_mc.x = bomb4;

cloud1_mc.x = cloud1;
cloud2_mc.x = cloud2;
cloud3_mc.x = cloud3;
cloud4_mc.x = cloud4;



}
2

2 Answers

0
votes

First off, put a few trace statements into the loop. That will tell you which line is firing the typeerror. For example:

trace("1");
carrot_mc.x = carrot;
trace("2");
carrot1_mc.x = carrot1;
trace("3");
carrot2_mc.x = carrot2;

et cetera. This will tell you exactly which line is wrong.

This error means something is undefined. Are all the movieclips defined in the .fla file? You might have misspelled something.

Also, shouldn't you reset all the random numbers from withing the loop function, not just at declaration?

0
votes

It appears that carrot[#]_mc/bomb[#]_mc/cloud[#]_mc are never created, so when you attempt to access one of their .x properties, you are trying to access a property of a null reference.

You need to instantiate the objects, before you use them.