This is a little snippet from a little flash game I’m working on:
This bit from my GamePlayScreen class:
public function handleKeyboard(e:KeyboardEvent):void {
if(e.type == KeyboardEvent.KEY_DOWN && e.keyCode == Keyboard.ENTER) {
var enemy:EnemyShip = new EnemyShip();
enemyships.push(enemy);
addChild(enemyships[enemyships.length-1]);
}
}
and this bit is causing the error (I marked the line with *)
for each (var enemy:EnemyShip in Game.gameplay.enemyships) {
if(this.hitTestObject(enemy)) {
*enemy.parent.removeChild(enemy);
}
gameplay is an instance of GamePlayScreen declared in my document class Game. I already traced enemy.parent and it told me it was [Object GamePlayScreen], yet the error tells me it is null?
This is the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Laser1/moveUp()[C:\Dev\Projects\Gamedev\Flash\classes\Laser1.as:23]
I tryed everything, tracing on every line, and still no results… Even when I use
if(Game.gameplay.contains(enemy) {
enemy.parent.removeChild(enemy);
}
it continues to stumble on this error again…
I am clueless…
A simple solution may be checking if the value is not null and the enemy still has a parent.