OK So i've narrowed down the problem to the bullets. When they are removed, their ENTER_FRAME event listeners are not removed. When I try to remove them, I get a null reference error caused by tempBullet.removeEventHandlers(); The function exists but flash says it doesnt? Can someone help me out?
Enemy.as
//Check to see if enemy hit a bullet
for(var i:int = 0;i < GameScreen(Container(this.parent).parent).bulletContainer.numChildren;i++){
var tempBullet:Bullet = Bullet(GameScreen(Container(this.parent).parent).bulletContainer.getChildAt(i));
var tempSelf:Enemy = this;
//Hit test for bullet
if(hitTestObject(tempBullet)){
this.gotoAndPlay("dead");
tempBullet.removeEventHandlers(); //THIS CAUSES FAILURE
tempSelf.removeEventHandlers();
GameScreen(Container(this.parent).parent).bulletContainer.removeChild(tempBullet); //remove bullet
Container(this.parent).removeChild(this); //remove enemy
}
Bullet.as
package barnabyjones{
import flash.display.MovieClip;
import flash.events.Event;
//Class
public class Bullet extends MovieClip{
//Public Var's
//Private Var's
private var movementSpeed:int = 25;
////////////////////////////////////////////////////////////////////////////////
//Constructor
////////////////////////////////////////////////////////////////////////////////
public function Bullet(_playerX:int, _playerY:int, _playerRotation:int):void{
//Set variables
this.x = _playerX;
this.y = _playerY;
this.rotation = _playerRotation;
//Add event listeners
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}//Constructor
////////////////////////////////////////////////////////////////////////////////
//Function: Enter frame event handler
////////////////////////////////////////////////////////////////////////////////
public function enterFrameHandler(e:Event):void{
//Move bullet towards cursor
this.x += movementSpeed * Math.sin(this.rotation * (Math.PI/180));
this.y -= movementSpeed * Math.cos(this.rotation * (Math.PI/180));
//Remove bullet if it goes off the stage
if(this.y < 0 || this.x < 0 || this.y > GameScreen(Container(this.parent).parent).screenHeight || this.x > GameScreen(Container(this.parent).parent).screenWidth){
//this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
Container(this.parent).removeChild(this);
}
}//addedToStageHandler
////////////////////////////////////////////////////////////////////////////////
//Function: Removes events handlers function
////////////////////////////////////////////////////////////////////////////////
public function removeEventHandlers():void{
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}//removeEventHandlers
}//Class: Bullet
}//Package