Home > Blockchain >  AS3 Restrict ship in visible stage area
AS3 Restrict ship in visible stage area

Time:12-06

Hey so i've been making a space shooter game and i created the movement but i'm trying to restricting the ship to the visible stage area and i can't figure out how to do that

also i'm sorry if my code it's really messy i'm really new at this

the stage size it's 1920 x 1080

here's the code of the movement

var leftKeyPressed: Boolean = false;
var rightKeyPressed: Boolean = false;
var upKeyPressed: Boolean = false;
var downKeyPressed: Boolean = false;

var xMoveSpeed: Number = 15;
var yMoveSpeed: Number = 15;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(e: KeyboardEvent): void
{
    if (e.keyCode == Keyboard.LEFT)
    {
        leftKeyPressed = true;

    }
    if (e.keyCode == Keyboard.RIGHT)
    {
        rightKeyPressed = true;

    }
    if (e.keyCode == Keyboard.UP)
    {
        upKeyPressed = true;

    }
    if (e.keyCode == Keyboard.DOWN)
    {
        downKeyPressed = true;

    }
}

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

function keyUpHandler(e: KeyboardEvent): void
{
    if (e.keyCode == Keyboard.LEFT)
    {
    leftKeyPressed = false;
    }
    if (e.keyCode == Keyboard.RIGHT)
    {
        rightKeyPressed = false;
    }
    if (e.keyCode == Keyboard.UP)
    {
        upKeyPressed = false;
    }
    if (e.keyCode == Keyboard.DOWN)
    {
        downKeyPressed = false;
    }
}

stage.addEventListener(Event.ENTER_FRAME, moveHero);

function moveHero(e: Event): void
{
    if (leftKeyPressed)
    {
        ship.x -= xMoveSpeed;
    }
    if (rightKeyPressed)
    {
        ship.x  = xMoveSpeed;
    }
    if (upKeyPressed)
    {
        ship.y -= yMoveSpeed;
    }
    if (downKeyPressed)
    {
        ship.y  = yMoveSpeed;
    }
} 

CodePudding user response:

Well, you can start with this.

// One map is much better than a separate variable for each and every key.
var keyMap:Object = new Object;

// Initial keymap values.
keyMap[Keyboard.UP] = 0;
keyMap[Keyboard.DOWN] = 0;
keyMap[Keyboard.LEFT] = 0;
keyMap[Keyboard.RIGHT] = 0;

// With the keymap you don't need these big event handlers,
// you need a single one with one line of code.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onUpDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUpDown);

function onUpDown(e:KeyboardEvent):void
{
    // The members of the object with the names of key codes
    // will be set to 1 in case of KEY_DOWN event or 0 in case of KEY_UP.
    keyMap[e.keyCode] = int(e.type == KeyboardEvent.KEY_DOWN);
}

stage.addEventListener(Event.ENTER_FRAME, moveHero);

// This will provide you with min and max values for both X and Y coordinates.
// You will probably need to adjust it because the ship have width and height.
var conStraints:Rectangle = new Rectangle(0, 0, 1920, 1080);

var xMoveSpeed:Number = 15;
var yMoveSpeed:Number = 15;

function moveHero(e:Event):void
{
    var anX:Number = ship.x;
    var anY:Number = ship.y;
    
    // Use keymap to figure the new coordinates.
    // Take into account all the relevant keys pressed.
    anX  = xMoveSpeed * (keyMap[Keyboard.RIGHT] - keyMap[Keyboard.LEFT]);
    anY  = yMoveSpeed * (keyMap[Keyboard.DOWN] - keyMap[Keyboard.UP]);
    
    // Now set the new ship coordinates with the regard to constraints.
    ship.x = Math.min(conStraints.right, Math.max(conStraints.left, anX));
    ship.y = Math.min(conStraints.bottom, Math.max(conStraints.top, anY));
} 

CodePudding user response:

I think this is the easiet solution, tell me if it worked or not

function keyDownHandler(e: KeyboardEvent): void
{
    if (e.keyCode == Keyboard.LEFT && ship.x > 15)
    {
        leftKeyPressed = true;

    }
    if (e.keyCode == Keyboard.RIGHT && ship.x < 1905)
    {
        rightKeyPressed = true;

    }
    if (e.keyCode == Keyboard.UP && ship.y > 15)
    {
        upKeyPressed = true;

    }
    if (e.keyCode == Keyboard.DOWN && ship.y < 1065)
    {
        downKeyPressed = true;

    }
}
  • Related