cannot access a property of a null object reference
Hi guys:)
I am trying to make a platform game with obstacles and coins. The coins are defined in a class. The code works fine until the player has lost all his health and is gameover. Then I get the error message that I have written above.
Here the code:
stop();
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.display.Sprite;
//vars (define events and functions)
var bulletHolder:Sprite=new Sprite();
addChild(bulletHolder);
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean=false;
var leftKeyIsDown:Boolean=false;
var upKeyIsDown:Boolean=false;
var downKeyIsDown:Boolean=false;
var spaceKeyIsDown:Boolean=false;
var cantShoot:Boolean=false;
var canShoot:Boolean=false;
var pistolCounter:int=0;
var pistolCountFrame:int=12;
var coinCount:int;
var hitObstacle:Boolean=false; // keeps track if obstacle is hit
var health=6;
health_txt.text=health.toString();
//propreties for player
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function PressAKey(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
rightKeyIsDown=true;
}
if(event.keyCode==Keyboard.LEFT)
{
rightKeyIsDown=true;
}
if(event.keyCode==Keyboard.UP)
{
upKeyIsDown=true;
}
if(event.keyCode==Keyboard.DOWN)
{
downKeyIsDown=true;
}
if(event.keyCode==Keyboard.SPACE)
{
spaceKeyIsDown=true;
}
}
function ReleaseAKey(event:KeyboardEvent):void
{
if(event.keyCode==Keyboard.RIGHT)
{
rightKeyIsDown=false;
}
if(event.keyCode==Keyboard.LEFT)
{
rightKeyIsDown=false;
}
if(event.keyCode==Keyboard.UP)
{
upKeyIsDown=false;
}
if(event.keyCode==Keyboard.DOWN)
{
downKeyIsDown=false;
}
if(event.keyCode==Keyboard.SPACE)
{
spaceKeyIsDown=false;
cantShoot=false;
}
}
function moveThePlayer(event:Event):void {
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
}
if(downKeyIsDown)
{
player_mc.gotoAndStop(2)
}
if(upKeyIsDown)
{
player_mc.gotoAndStop (3);
}
if(spaceKeyIsDown)
{
player_mc.gotoAndStop(3);
}
if(pistolCounter<pistolCountFrame)
{
pistolCounter++
}
if(pistolCounter>=pistolCountFrame)
{
canShoot=true;
pistolCounter=0;
}
if(spaceKeyIsDown&&!cantShoot&&canShoot)
{
var bullet_mc:MovieClip=new Bullet();
bullet_mc.x=player_mc.x
bullet_mc.y=player_mc.y
bullet_mc.rotation=player_mc.rotation_mc.rotation
bulletHolder.addChild(bullet_mc);
cantShoot=true;
canShoot=false;
}
}
if(rightKeyIsDown)
{
player_mc.rotation_mc.rotation=0
}
if(leftKeyIsDown)
{
player_mc.rotation_mc.rotation=180
}
//defines player
stage.addEventListener(KeyboardEvent.KEY_UP, run);
function run(e:KeyboardEvent):void{
player_mc.gotoAndStop(1);
}
stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);
var speed=10;
var moveright=false;
function checkkeysdown(mykey:KeyboardEvent) {
if (mykey.keyCode==Keyboard.RIGHT) {
moveright=true;
}
}
function checkkeysup(mykey:KeyboardEvent) {
if (mykey.keyCode==Keyboard.RIGHT) {
moveright=false;
}
}
//defines obstacles
stage.addEventListener(Event.ENTER_FRAME, gameloop);
function gameloop(e:Event):void{
obstacle_mc.x-=20;
if (obstacle_mc.x<-100){
obstacle_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(obstacle_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.removeEventListener(Event.ENTER_FRAME, axtloop);
gotoAndStop(1, "Scene 3");
}
}
}
stage.addEventListener(Event.ENTER_FRAME, axtloop);
function axtloop(e:Event):void{
axt_mc.x-=20;
if (axt_mc.x<-100){
axt_mc.x=650;
hitObstacle=false;
}
if (player_mc.hitTestObject(axt_mc)) {
if (hitObstacle==false){ // only subtract health if hitObstacle is false
health--;
}
hitObstacle=true;
health_txt.text=health.toString();
if (health<=0){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, moveThePlayer);
stage.removeEventListener(KeyboardEvent.KEY_UP, run);
stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);
stage.removeEventListener(Event.ENTER_FRAME, axtloop);
gotoAndStop(1, "Scene 3");
}
}
//counts coins
coinCount_txt.text="coins:"+coinCount;
}
The code for the coins:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.net.dns.AAAARecord;
public class Coin extends MovieClip
{
var player_mc:MovieClip;
public var MainTimeLine=MovieClip(root);
public function Coin()
{
// constructor code
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player_mc=MovieClip(root).player_mc
if(this.hitTestObject(player_mc))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MainTimeLine.coinCount++;
}
}
}
}
I really would appreciate some help because I am trying now since 5 days to solve the problem without success.
Many thanks in advance
