dimanche 28 février 2016

AS3 Error 2025 When Game Restart

I'm trying to remove objects when game is over by using the removeChild function. But I got error 2025 and the debugger freeze when I restart the game. Any help is greatly appreciated!

import control.Joystick;
import objects.PauseButton;

public class Engine extends MovieClip {

    public function Engine() {

    private var numStars: int = 80;
    public static var enemyList: Array = new Array();
    private var ourShip: Ship;
    private var joystick: Joystick;
    private var scoreHUD: ScoreHUD;
    public var pausebutton: PauseButton;

    public function GameStart(evt: Event): void {

        ourShip = new Ship(stage);
        ourShip.x = stage.stageWidth / 2;
        ourShip.y = stage.stageHeight / 2;
        ourShip.addEventListener("hit", shipHit, false, 0, true);
        stage.addChild(ourShip);

        joystick = new Joystick(300, 70, ourShip);
        stage.addChild(joystick);

        scoreHUD = new ScoreHUD(stage);
        stage.addChild(scoreHUD);

        pausebutton = new PauseButton(stage);
        stage.addChild(pausebutton);

        for (var i: int = 0; i < numStars; i++) {
            stage.addChildAt(new Star(stage), stage.getChildIndex(ourShip));
        }

        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

        function loop(e: Event): void {

            if (Math.floor(Math.random() * 20) == 5) {
                var enemy: Stinger = new Stinger(stage, ourShip);
                enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
                enemy.addEventListener("killed", enemyKilled, false, 0, true);
                enemyList.push(enemy);
                stage.addChild(enemy);
            }

            else if (Math.floor(Math.random() * 80) == 5) {
                var enemy2: Stinger2 = new Stinger2(stage, ourShip);
                enemy2.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
                enemy2.addEventListener("killed", enemyKilled, false, 0, true);
                enemyList.push(enemy2);
                stage.addChild(enemy2);
            }
        }

        function enemyKilled(e: Event) {
            scoreHUD.updateKills(1);
            scoreHUD.updateScore(e.currentTarget.points);
        }

        function removeEnemy(e: Event) {
            enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
        }

        function shipHit(e: Event) {
            scoreHUD.updateShields(-1);
        }

        stage.addEventListener("gameOVER", GameOver);
        function GameOver(e: Event) {
            new GameOverMenu(stage).load();
            removeEventListener(Event.ENTER_FRAME, loop);
            stage.removeChild(ourShip);
            stage.removeChild(joystick);
            stage.removeChild(scoreHUD);
            stage.removeChild(pausebutton);
        }

    }

}

I also tried to change the removeChild to the script below but still no luck.

if(ourShip.parent) {
    ourShip.parent.removeChild(ourShip);
}

Aucun commentaire:

Enregistrer un commentaire