ActionScript 3: Stop onEnterFrame event

Status
Niet open voor verdere reacties.

Lunanoko

Gebruiker
Lid geworden
29 sep 2005
Berichten
26
Hallo dames en heren,

Bij het programmeren van een simpele game in ActionScript 3 loop ik tegen de volgende foutmelding aan als ik de game uitvoer:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at actionscript::EnemyFighter/onEnteringFrame()

Dit komt (denk ik) doordat dit object een functie aanroept binnen de onEnteringFrame functie waaraan hij zichzelf meegeeft als parameter. Echter, omdat dit object al van de Stage is verwijderd kan dat niet meer en geeftie deze fout.

Normaal kan ik onEnterFrame stoppen door deze weg te halen als het betreffende object van de stage wordt gehaald, maar dat wil niet lukken dit keer...

Ik heb al geprobeerd de functies in omgekeerde volgorde in de code te plaatsen, maar dat hielp niet.

Ik heb de class ook al een variable 'exists' meegegeven, en daarna de onEnteringFrame functie laten checken of deze wel 'true' is. (de onRemoved functie zette deze variabele op 'false'. Dat hielp ook niet, dus het lijkt gewoon alsof de onEnteringFrame áltijd eerder dan de onRemoved functie. Is dit misschien om te draaien? Of weet iemand een andere oplossing?

Hier is de code van de Class:

Code:
package actionscript {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import actionscript.Docla;
		
	public class EnemyFighter extends Sprite {									// Inherit all of Sprite's properties and methods
		
		/*****************************************
		*********** PRIVATE PROPERTIES ***********
		*****************************************/
		
		private var _velocity:int;	// This determines the speed of the Fighter
	
		/*****************************************
		************* INITIALIZATION *************
		*****************************************/
		
		public function EnemyFighter() {
			addEventListener(Event.ADDED_TO_STAGE, init);						// When the Fighter is added to the stage it should call the init method to initialize itself
		}
		
		/*****************************************
		************ PRIVATE METHODS *************
		*****************************************/
		
		/*
		This method initialized the EnemyFighter
		whenever it receives a sign that it has been
		added to the stage
		*/
		private function init(e:Event):void {
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);				// Listen for the REMOVED_FROM_STAGE event, then call onRemoved
			addEventListener(Event.ENTER_FRAME, onEnteringFrame);				// Listen for the ENTER_FRAME event, then call onEnteringFrame
			this.x = -60;														// Place the Fighter off the screen, so that to the player it doesn't pop out of nowhere
			this.y = Math.random() * 220 + 30;									// Place the Fighter at a random height
			this._velocity = Math.random() * 4 + 5;								// Gives the Fighter a random speed between 4 and 9
		}
		
		/*
		This method tells the plane what to do when
		a new ENTER_FRAME event has occured
		*/
		private function onEnteringFrame(e:Event):void {
			e.target.parent.checkCollisionWithBuilding(this);					// Calls a Docla method that checks whether the plane hits the building
			e.target.parent.offScreenCheck(this);								// Calls a Docla method that checks whether the plane has left the screen
			this.x += _velocity;												// Move the plane to the right at it's pre-determined speed
		}
		
		/*
		This method is called whenever the Fighter
		is removed from the stage. It removes all
		of it's event listeners so that no errors
		are caused by 'ghost events'
		*/
		private function onRemoved(e:Event):void {
			removeEventListener(Event.ENTER_FRAME, onEnteringFrame);			// Remove the given event listener
		}
	}
	
}

... en hier is de functie uit 'Docla' die aangeroepen wordt waardoor de fout optreed:

Code:
		public function checkCollisionWithBuilding(enemy:Sprite):void {
			if (building.hitTestPoint(enemy.x + enemy.width/2, enemy.y + enemy.height/2, true) ||	// If the right side of the enemy OR the bottom of the enemy OR ...
				building.hitTestPoint(enemy.x - enemy.width/2, enemy.y - enemy.height/2)) {			// ... the left  side of the enemy OR the top of the enemy hits the building ...
				removeChild(enemy);												// ... remove the enemy and ...
				updateScore(-20);												// ... update the player's score
			}
		}

Heeft iemand een idee?
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan