Jump to content


Photo

odd null object reference error


  • Please log in to reply
1 reply to this topic

#1 beefwellington

beefwellington

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 09 February 2010 - 03:22 PM

Hi Everyone,
I've been working on my final game project for class and have almost everything working except when I shoot enemies, I get the following error when they disappear:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at barnabyjones::Enemy/enterFrameHandler()[C:\Users\Kevin\Desktop\RIT\Interactive Digital Media\Game\barnabyjones\Enemy.as:44]

Line 45 of Enemy.as is:
for(var i:int = 0;i < GameScreen(Container(this.parent).parent).bulletContainer.numChildren;i++){

I'm not sure why this would be thrown and has had me stumped the past few days. If anyone could take a look at my code it would be greatly appreciated. My code is attached

Attached Files



#2 beefwellington

beefwellington

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 09 February 2010 - 10:26 PM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users