I've been working on this game (click to see) for awhile and, although it's doing what I want it to do so far, I'm still getting errors. The only specific error I'm getting is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at GameDocumentClass/checkEnemyCollisions()
at GameDocumentClass/enterFrameHandler()
The enemies disappear after their health is depleted and the bullets also disappear when they impact something, so I am not sure why I'm getting any error. Here is the code for the "checkEnemyCollisions" function:
public function checkEnemyCollisions() :void{
var zeldaHit:ZeldaHit = new ZeldaHit();
var soundChannel2:SoundChannel = new SoundChannel;
var bullet:MovieClip;
var enemy:MovieClip;
var impact = new Impact();
for (var i:int = 0; i < bulletArray.length; i++){
for(var j:int = 0; j < enemyArray.length; j++){
bullet = bulletArray[i];
enemy = enemyArray[j];
if (enemy.hitTestPoint(bullet.x, bullet.y, true)) {
soundChannel2 = zeldaHit.play();
stage.removeChild(bullet);
stage.addChild(impact);
impact.x = bullet.x;
impact.y = bullet.y;
enemy.enemyHealth --;
enemy.gotoAndPlay("enemyHit");
bulletArray.splice(i,1);
}
}
}
}
I've tried tracing every variable in this code and nothing is returned null. It also mentions my "enterFramHandler" function and here it is:
private function enterFrameHandler(event:Event):void
{
checkRupeeCollisions();
checkEnemyCollisions();
checkGroundCollisions();
fire();
}
Now, the checkRupeeCollisions function and checkEnemyCollisions function is exactly the same, with the exception of any "gotoAndPlay" function, since there is no hit animation for the rupees right now, but I do not get an error when I hit a rupee (even after trying adding a health value to them). I am guessing the problem may lie in my enemy class functions which is here:
package{
import flash.display.MovieClip;
import fl.transitions.*;
import flash.events.Event;
import flash.media.*
import GameDocumentClass;
public class Enemy extends MovieClip
{
private var speed = 1;
public var enemyHealth:Number = 10;
private var enemyTimer = 0;
public function Enemy()
{
addEventListener(Event.ENTER_FRAME, updateEnemy);
this.addEventListener(Event.ENTER_FRAME, removeEnemy);
}
private function updateEnemy(event:Event)
{
moveEnemy();
if (enemyTimer >= 30){
speed = speed * -1;
enemyTimer = 0;
}
}
private function moveEnemy():void
{
this.x -= speed;
enemyTimer ++;
}
private function removeEnemy(e:Event)
{
var zeldaKill:ZeldaKill = new ZeldaKill();
var soundChannel3:SoundChannel = new SoundChannel;
if(enemyHealth <= 0){
soundChannel3 = zeldaKill.play();
trace(GameDocumentClass.enemyArray);
stage.removeChild(this);
removeEventListener(Event.ENTER_FRAME, removeEnemy);
}
}
}
}
Does anyone see anything wrong with this? I know it's a lot to ask, but I would appreciate any try from anyone. Thanks!