I’m trying to have a bullet Sprite remove itself from the stage once it is off the stage, but Flash keeps throwing #1009 errors at me. I’ve looked over the code several times and don’t understand what’s wrong.
Bullet code:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.*;
public class Bullet extends Sprite {
private var bullet_direction;
private var bullet_speed;
public function Bullet(xLoc:Number, yLoc:Number, angle:Number, speed:Number) {
x = xLoc;
y = yLoc;
bullet_direction = angle;
bullet_speed = speed;
addEventListener(Event.ENTER_FRAME, BulletMovement,false,0,true);
}
public function BulletMovement(e:Event) {
y -= bullet_speed * Math.sin(bullet_direction);
x += bullet_speed * Math.cos(bullet_direction);
if ( x > 500 || x < -20 || y > 660 || y < -20 ) {
DestroyBullet();
}
}
public function DestroyBullet():void {
removeEventListener(Event.ENTER_FRAME, BulletMovement);
(stage as MovieClip).removeChild(this);
}
}
}
Error message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/DestroyBullet()
at Bullet/BulletMovement()
Any help would be much appreciated.