AS3: Multiple key presses allows violation of if/then

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

AS3: Multiple key presses allows violation of if/then

Postposted on Tue Jan 31, 2012 4:50 pm

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

Code: Select all
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;
   }
   
   
   
   
   
}
StefanVonS
Gerbil Elite
 
Posts: 547
Joined: Mon Aug 14, 2006 2:51 pm
Location: Strong Badia

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

Postposted on Tue Jan 31, 2012 5:11 pm

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!
Last edited by yogibbear on Tue Jan 31, 2012 5:24 pm, edited 2 times in total.
Core i7 4770K | eVGA GTX770 SC ACX | 16GB DDR3 2133mhz | Asus Z87-PLUS | Corsair HX650 | Fractal Define R4 | Samsung 840 Pro 256GB | Windows 8 x64
yogibbear
Gerbil Elite
Gold subscriber
 
 
Posts: 671
Joined: Fri Feb 08, 2008 11:30 am

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

Postposted on Tue Jan 31, 2012 5:19 pm

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.
Entroper
Entroper
Gerbil
Gold subscriber
 
 
Posts: 59
Joined: Mon Nov 11, 2002 4:49 am
Location: Virginia

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

Postposted on Tue Jan 31, 2012 6:01 pm

I love you guys... thank you!
StefanVonS
Gerbil Elite
 
Posts: 547
Joined: Mon Aug 14, 2006 2:51 pm
Location: Strong Badia


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 2 guests