lundi 5 décembre 2016

Runtime errors in my Actionscript 3 game (#1009)

I am working on a game for a school project, it's a basic breakout game. For the most part it functions as intended, however it breaks with runtime errors in a few specific areas, and I can't seem to find where the fix needs to go after staring at it for a fair amount of the semester.

I encounter the main error (#1009) on adding an event listener through a function ("beginCode") that is supposed to run the bulk of the code either when advancing to the next level or restarting from a game-over state. The event listener is for another function ("checkLevel") that verifies what level the game is advancing to and generates the next level.

I looked through my code and tried to verify that all of the listeners were added and removed in all the right places, but can't seem to find why this one is getting caught up (I can tell where, it's line 26 of my second frame). Below is the entirety of my code, on the off chance that it is that prevalent of a problem. Sorry for the long post, I wanted to make sure I described my problem as best I could. I have marked the line at which I am experiencing the error with asterisks.

Frame 1

import flash.events.MouseEvent;

var currentLvl:int = 1;

var lvl1Code:Array = new Array(1,1,1,1,1,1,1);

var lvl2Code:Array = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1);

var lvl3Code:Array = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);

var lvl4Code:Array = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);

var lvl5Code:Array = new Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);

var lvlArray:Array = new Array(lvl1Code, lvl2Code, lvl3Code, lvl4Code, lvl5Code);

txtStart.text = "CLICK ANYWHERE TO BEGIN";

txtTitle.text = "BREAKOUT";

stop();

addEventListener(MouseEvent.CLICK, beginGame);

function beginGame(event:MouseEvent):void
{
    gotoAndPlay(2);
}

stage.removeEventListener(MouseEvent.CLICK, resetGame);

Frame 2

stop();

import flash.events.Event;

import flash.display.MovieClip;

var brickAmt:int = 0;

var ballXSpeed:Number = 6;

var ballYSpeed:Number = 6;

var lives:int = 3;

var score:int = 0;

var gameOver:Boolean = false;

function beginCode(event:MouseEvent):void
{
    stage.removeEventListener(MouseEvent.CLICK, beginGame);
    stage.removeEventListener(MouseEvent.CLICK, beginCode);
    stage.removeEventListener(MouseEvent.CLICK, resetGame);
    mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
    mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
    ***addEventListener(Event.ENTER_FRAME, checkLevel);***
    txtStart.text = '';
    txtTitle.text = '';
}

function movePaddle(event:Event): void
{
    mcPaddle.x = mouseX - mcPaddle.width / 2;
    if (mouseX < 25 + (mcPaddle.width))
    {
        mcPaddle.x = 25 + (mcPaddle.width / 2);
    }
    if (mouseX > 575 - (mcPaddle.width / 2))
    {
        mcPaddle.x = 575 - (mcPaddle.width / 2);
    }
}

function moveBall(event:Event):void
{
    mcBall.x += ballXSpeed;
    mcBall.y += ballYSpeed;
    if (mcBall.x > 575 - mcBall.width)
    {
        mcBall.x = 575 - mcBall.width;
        ballXSpeed *= -1;
    }
    if (mcBall.x < 25 + mcBall.width)
    {
        mcBall.x = 25 + mcBall.width;
        ballXSpeed *= -1;
    }
    if (mcBall.y > 400 - mcBall.height)
    {       
        ballYSpeed *= -1;
        lives--;
        score -= 50;
        if (lives <= 0)
        {
            gameOver = true;
            gotoAndStop('lose');
            mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
            mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
            removeEventListener(Event.ENTER_FRAME, checkLevel);
            removeEventListener(Event.ENTER_FRAME, textUpdater);
            brickAmt = 0;
        }
    }
    if (mcBall.y <= 25 + mcBall.height)
    {
        ballYSpeed *= -1;
    }
    if (mcBall.hitTestObject(mcPaddle))
    {
        calcBallAngle();
    }
}

function calcBallAngle():void
{
    var ballPosition:Number = mcBall.x - mcPaddle.x;
    var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width));
    ballXSpeed = hitPercent * 10;
    ballYSpeed *= -1;
}

function makeLvl():void
{
    if (currentLvl > lvlArray.length)
    {
        gameOver = true;
        mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
        mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
        removeEventListener(Event.ENTER_FRAME, checkLevel);
        gotoAndStop("win");
    }
    var arrayLength:int = lvlArray[currentLvl-1].length;
    var brickRow:int = 0;
    for (var i:int = 0; i < arrayLength; i++)
    {
        if (lvlArray[currentLvl-1][i] == 1)
        {
            var brick:MovieClip = new Brick();
            brick.x = 75 + (i - brickRow * 7) * 75;
            brick.y = 35 + brickRow * 20;
            for (var c:int = 1; c <= 10; c++)
            {
                if (i == c * 7 - 1)
                {
                    brickRow++;
                }
            }
            addChild(brick);
        }
    }
}

function checkLevel(event:Event):void
{
    if (brickAmt == 0)
    {
        currentLvl++;
        txtStart.text = "CLICK TO BEGIN";
        makeLvl();
        mcBall.x = 300;
        mcBall.y = 225;
        mcPaddle.x = 300;
        ballXSpeed += 1;
        ballYSpeed += 1;
        mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
        mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
        removeEventListener(Event.ENTER_FRAME, checkLevel);
        stage.addEventListener(MouseEvent.CLICK, beginCode);
        lives++;
    }
}

function textUpdater(event:Event):void
{
    txtLives.text = "LIVES: " + String(lives);
    txtScore.text = "SCORE: " + String(score);
    txtLevel.text = "LEVEL: " + String(currentLvl);
    if (gameOver == true)
    {
        txtLevel.text = "";
        txtLives.text = "";
        txtScore.text = "";
    }
}

function resetGame(event:MouseEvent):void
{
    gameOver = true;
    stage.removeEventListener(Event.ENTER_FRAME, beginCode);
    stage.removeEventListener(Event.ENTER_FRAME, makeLvl);
    stage.removeEventListener(Event.ENTER_FRAME, checkLevel);
    stage.removeEventListener(Event.ENTER_FRAME, textUpdater);
    stage.removeEventListener(MouseEvent.CLICK, resetGame);
    gotoAndPlay(1);
}

stage.addEventListener(Event.ENTER_FRAME, textUpdater);

stage.addEventListener(MouseEvent.CLICK, beginCode);

makeLvl();

Frame 3, "lose"

import flash.events.MouseEvent;

stage.addEventListener(MouseEvent.CLICK, resetGame);

currentLvl = 1;

Frame 4, "win"

stage.addEventListener(MouseEvent.CLICK, resetGame);

currentLvl = 1;

Brick.as (Brick Class File)

package
{
    import flash.display.*;
    import flash.events.*;
    public class Brick extends MovieClip
    {
        private var _root:MovieClip;
        public function Brick()
        {
            addEventListener(Event.ADDED, beginClass);
            addEventListener(Event.ENTER_FRAME, enterFrameEvents);
        }
        private function beginClass(event:Event):void
        {
            _root = MovieClip(root);
            _root.brickAmt++;
        }
        private function enterFrameEvents(event:Event):void
        {
            if (this.hitTestObject(_root.mcBall))
            {
                _root.ballYSpeed *= -1;
                this.parent.removeChild(this);
                removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
                _root.brickAmt--;
                _root.score += 100;
            }
            if (_root.gameOver)
            {
                this.parent.removeChild(this);
                removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
            }
        }
    }
}

Thank you all in advance.

Aucun commentaire:

Enregistrer un commentaire