Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-21-2012, 10:50 AM   #1
NC91
Registered User
 
Join Date: Aug 2012
Posts: 3
Default 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);

}
}
}
NC91 is offline   Reply With Quote
Old 08-21-2012, 12:30 PM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,332
Default

visible is misspelled.
[afz]snickelfitz is offline   Reply With Quote
Old 08-21-2012, 02:12 PM   #3
NC91
Registered User
 
Join Date: Aug 2012
Posts: 3
Default

Oh yeah didnt realise but thats not the problem, the problem seems to be that the arrow vaiarble isnt being recognised as havin any values to it.
NC91 is offline   Reply With Quote
Old 08-21-2012, 04:21 PM   #4
NC91
Registered User
 
Join Date: Aug 2012
Posts: 3
Default

Just tried using arrowContainer instead of arrow and the collision works but its not if the player hits the arow it all over the screen apart from the sides and that only works if an enemy hasn't spawned there.
NC91 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:25 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.