Zoske
Zoske

Reputation: 39

AS3 - Ball overspeed

I'm making a little app with Flash Pro CS6 and I made this ball that moves using the arrow keys (made using the Code Sippets) at a speed of 4 pixels per frame. The problem is that, whenever I die I get to another frame there I can hit replay and try again the game, but this time, the speed of the ball is 8 pixels/frame, and this number keeps incresing everytime I replay the game. Is there any way to fix that? Here is the snippet I'm using:

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;

ball.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey); //I think the problem might be this "Enter Frame"
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
    ball.y -=  4;
}
if (downPressed)
{
    ball.y +=  4;
}
if (leftPressed)
{
    ball.x -=  4;
}
if (rightPressed)
{
    ball.x +=  4;
}
}

function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
    case Keyboard.UP :
        {
            upPressed = true;
            break;


        };
    case Keyboard.DOWN :
        {
            downPressed = true;
            break;


        };
    case Keyboard.LEFT :
        {
            leftPressed = true;
            break;


        };
    case Keyboard.RIGHT :
        {
            rightPressed = true;
            break;


    }
}
};

function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
    case Keyboard.UP :
        {
            upPressed = false;
            break;


        };
    case Keyboard.DOWN :
        {
            downPressed = false;
            break;


        };
    case Keyboard.LEFT :
        {
            leftPressed = false;
            break;


        };
    case Keyboard.RIGHT :
        {
            rightPressed = false;
            break;


    }
}
};

Upvotes: 0

Views: 261

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

The way it looks to me, you create a new ball instance each time you gotoAndPlay the first frame - but there still is an event listener attached to the original one, even after it is removed from the stage.

So on each new ENTER_FRAME, the method is called not only once, but once for each instance of the ball. The solution is to remove the event listener when "you die", or to use weak references, so that the ball instance can be removed cleanly.

BTW this is the short version of your script - there is no need for all those if- and switch-statements, and instance variables:

var keyPressed:int = -1;
ball.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_SetKeyPressed(event:KeyboardEvent):void {
    keyPressed = event.keyCode;
}

function fl_UnsetKeyPressed(event:KeyboardEvent):void {
    keyPressed = -1;
}

function fl_MoveInDirectionOfKey(event:Event) {
    ball.y += keyPressed == Keyboard.UP ? -4 : keyPressed == Keyboard.DOWN ? 4 : 0;
    ball.x += keyPressed == Keyboard.LEFT ? -4 : keyPressed == Keyboard.RIGHT ? 4 : 0;
}

Upvotes: 1

Related Questions