I am making a game where you control a turret that shoots falling objects out of the sky. I am having problems with the hitests. The game still runs but when the bullet reaches the projectile it does not trace hit and in the output this appears once the game is opened: TypeError: Error #1009: Cannot access a property or method of a null object reference. at index/loop()
If someone could get back to me as soon as possible it would be greatly appreciated.
Here is my code:
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.ui.Mouse;
import flash.events.Event;
public class index extends MovieClip
{
public var army:Array;
public var projectile: Projectile;
public var avatar:Avatar;
public var gameTimer:Timer;
public var bullet:Bullet;
public var spawnLimit:Number;
public function index()
{
spawnLimit = 0.04;
avatar = new Avatar();
addChild( avatar );
avatar.x = mouseX;
avatar.y = mouseY;
army = new Array();
gameTimer = new Timer( 25 );
gameTimer.addEventListener( TimerEvent.TIMER, onTick );
gameTimer.start();
addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event){
if (bullet.hitTestObject(projectile)){
trace("hit");
}
}
function onTick( timerEvent:TimerEvent )
{
Mouse.hide();
avatar.x = mouseX;
avatar.y = mouseY;
var randomX:Number = Math.random() * stage.stageWidth;
var newProjectile: projectile = new Projectile( randomX, -40 );
if ( Math.random() < spawnLimit )
{
army.push( newProjectile );
addChild( newProjectile );
}
for each (var projectile: projectile in army) {
projectile.moveDownABit();
}
}
}
}