Page 1 of 1

AS3: Multiple key presses allows violation of if/then

Posted: Tue Jan 31, 2012 4:50 pm
by StefanVonS
I'm on to learning AS3, and in trying to make a simple scroller, have run across a problem I can't fix and Google searches haven't been fruitful.

At the current stage of development, this little app has an "inner boundary" where the player object is restricted. Attempting to move beyond, scrolls the background... you know the drill.

However, if two keys are pressed (Down & Right, for example) the boundary is ignored, but player snaps back into position after letting go of the keys. What am I missing?

Thanks in advance to anyone that has a second to look at it!

Code below, and code in action here

import flash.events.Event;
import flash.events.KeyboardEvent;

var playerHalfHeight:uint = player.height/2;
var playerHalfWidth:uint = player.width/2;
var stageHalfHeight:uint = stage.stageHeight/2;
var stageHalfWidth:uint = stage.stageWidth/2;
var backgroundHalfHeight:uint = background.height/2;
var backgroundHalfWidth:uint = background.width/2;
var vx:Number = 0;
var vy:Number = 0;
var playerInnerRightBoundary:uint = stageHalfWidth + (stageHalfWidth/2);
var playerInnerLeftBoundary:uint = stageHalfWidth - (stageHalfWidth/2);
var playerInnerTopBoundary:uint = stageHalfHeight - (stageHalfHeight/2);
var playerInnerBottomBoundary:uint = stageHalfHeight + (stageHalfHeight/2);


stage.addEventListener(Event.ENTER_FRAME,eachFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyHandlerDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyHandlerUp);

function keyHandlerDown (event:KeyboardEvent):void
{
   if (event.keyCode == Keyboard.LEFT)
   {
      vx = -5;
   }
   if (event.keyCode == Keyboard.RIGHT)
   {
      vx = 5;
   }
   if (event.keyCode == Keyboard.UP)
   {
      vy = -5;
   }
   if (event.keyCode == Keyboard.DOWN)
   {
      vy = 5;
   }
}

function keyHandlerUp (event:KeyboardEvent):void
{
   if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
   {
      vx = 0;
   }
   if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
   {
      vy = 0;
   }
}

function eachFrame (event:Event):void
{
   player.rotation += 1;
   player.x += vx;
   player.y += vy;
   
   if (player.x > playerInnerRightBoundary-playerHalfWidth)
   {
      player.x = playerInnerRightBoundary-playerHalfWidth;
      background.x += -vx;
   }
   
   else if (player.x < playerInnerLeftBoundary+playerHalfWidth)
   {
      player.x = playerInnerLeftBoundary + playerHalfWidth;
      background.x += -vx;
   }
   
   else if (player.y < playerInnerTopBoundary + playerHalfHeight)
   {
      player.y = playerInnerTopBoundary + playerHalfHeight;
      background.y += -vy;
   }
   
   else if (player.y > playerInnerBottomBoundary-playerHalfHeight)
   {
      player.y = playerInnerBottomBoundary - playerHalfHeight;
      background.y += -vy;
   }
   
   if (background.x + backgroundHalfWidth < stage.stageWidth)
   {
      background.x = stage.stageWidth - backgroundHalfWidth;
   }
   
   else if (background.x - backgroundHalfWidth > 0)
   {
      background.x = 0 + backgroundHalfWidth;
   }
   
   else if (background.y - backgroundHalfHeight > 0)
   {
      background.y = 0 + backgroundHalfHeight;
   }
   
   else if (background.y + backgroundHalfHeight < stage.stageHeight)
   {
      background.y = stage.stageHeight - backgroundHalfHeight;
   }
   
   
   
   
   
}

Re: AS3: Multiple key presses allows violation of if/then

Posted: Tue Jan 31, 2012 5:11 pm
by yogibbear
Only your top and bottom boundaries stop working. The sides still work. i.e. that narrows down where the culprit is. I don't actually do coding, just trying to be as helpful as I can. :)

EDIT:

What would happen if you change all the else ifs in the last function to be if { } followed by another if { }. i.e. instead of it exiting the if loop after confirming an edge hit and exiting the check it would continuing to check them all...

EDIT #2: ninja'd!

Re: AS3: Multiple key presses allows violation of if/then

Posted: Tue Jan 31, 2012 5:19 pm
by Entroper
It's your use of "else if" in eachFrame(). The line "else if (player.x < playerInnerLeftBoundary+playerHalfWidth)" should have the "else" removed, and similarly for the lines that check player.y. The "else" is causing the code to skip the y-boundary checks if either of the x-boundary checks fail. The same thing is happening in the background code, so remove those "elses" as well.

Re: AS3: Multiple key presses allows violation of if/then

Posted: Tue Jan 31, 2012 6:01 pm
by StefanVonS
I love you guys... thank you!