TypeError: Error #1009: Cannot access a property or method of a null object reference
Can someone please tell me why my collision detection isnt working and why this error constantly comes up. The problem is at the action function.
heres my code:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import classes.Robin;
import classes.Arrow;
import classes.ArrowEvent;
public class Main extends Sprite {
private var arrowContainer:Sprite ;
// Variables used for loop counters and temporary objects
private var i:Number, j:Number, distance:Number, pointX:Number, pointY:Number;
private var arrow:Arrow;
private var player:Robin;
// Timers
private var shootArrow:Timer;
private var scoreTimer:Timer;
private var score:int = 0;
public function Main() {
reset_btn.visible = false;
GameOverBox.visible = false ; // static text box on the stage in the .fla
instructBox.visible = true ; // static text box on the stage in the .fla
this.player = new Robin(275, 350);
start_btn.addEventListener(MouseEvent.CLICK, startGame);
addEventListener(Event.ENTER_FRAME, action);
addEventListener(KeyboardEvent.KEY_DOWN, keypress);
}
private function startGame(e:MouseEvent) {
instructBox.visible = false;
start_btn.visible = false;
// create and add containers for the arrows and beach balls
this.arrowContainer = new Sprite ();
addChild (this.arrowContainer);
addChild(this.player);
// create a timer & event listener to shoot a new enemy every second
this.shootArrow = new Timer (500); // timer ticks every 1 s. and never ceases
this.shootArrow.addEventListener(TimerEvent.TIMER, shoot);
this.shootArrow.start();
this.scoreTimer = new Timer (500);
this.scoreTimer.addEventListener(TimerEvent.TIMER, scorer);
this.scoreTimer.start();
// add event listeners to hear when an arrow or a ball is killed or goes off stage
addEventListener(ArrowEvent.KILLED, deadEnemy);
}
public function action(e:Event) {
this.player.move();
if (arrow[arrowContainer].hitTestObject(player)) {
GameOverBox.visble = true;
}
}
private function keypress(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.LEFT) {
this.player.left();
} else if (e.keyCode == Keyboard.RIGHT) {
this.player.right();
}
}
// event handler to shoot a new enemy when the enemy shoot timer rings
private function shoot (e:TimerEvent):void {
// calculate a random starting position
var startPos:Number = Math.floor(Math.random()*stage.stageWidth);
// calculate a random speed between 2 and 6
var speed:Number = Math.floor(Math.random()*4+2);
// create the new ball
var enemy:Arrow = new Arrow(startPos, 42, speed, stage.stageWidth, stage.stageHeight);
// add it to the container
this.arrowContainer.addChild(enemy);
enemy.name = "Arrow " + Arrow.createdCount;
}
// event handler to respond to a dead enemy event
private function deadEnemy(e:ArrowEvent) {
var obj:Arrow = (e.object as Arrow);// creeate a temporary object and use it to point to the object passed in the event
// remove the dead ball from its container
this.arrowContainer.removeChild(obj);
}
// Event handler to countdown time to zero
private function scorer(e:TimerEvent) {
score += 1;
scoreCounter.text = score.toString();
}
// Function to return a formatted time string from a Date object
// Event handler to deal with the end of game state
private function endGame(e:TimerEvent) {
this.scoreTimer.stop();
this.shootArrow.stop();
this.shootArrow.removeEventListener(TimerEvent.TIM ER, shoot);
this.scoreTimer.removeEventListener(TimerEvent.TIM ER, scorer);
removeEventListener(Event.ENTER_FRAME, action);
removeEventListener(KeyboardEvent.KEY_DOWN, keypress);
//WHILE Arrows left on the stage DO
while (this.arrowContainer.numChildren >0) {
// set temporary object to point to current Arrow
var arrow:Arrow =this.arrowContainer.getChildAt(0) as Arrow;
//kill the Arrow
arrow.kill(true);
}
// Make the game over text visible
GameOverBox.visible = true;
reset_btn.visible = true;
player.visible = false;
}
private function reset(e:MouseEvent) {
GameOverBox.visible = false;
player.visible = true;
this.scoreTimer.reset();
this.scoreTimer.start();
this.shootArrow.start();
this.shootArrow.addEventListener(TimerEvent.TIMER, shoot);
this.scoreTimer.addEventListener(TimerEvent.TIMER, scorer);
addEventListener(Event.ENTER_FRAME, action);
addEventListener(KeyboardEvent.KEY_DOWN, keypress);
}
}
}
|