Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-25-2011, 12:11 AM   #1
Palifael
Registered User
 
Join Date: Sep 2011
Posts: 10
Default [AS3] Platform Game: Better Response from Keypresses

I have a problem with a Platform Game I'm making, when I press the an Arrow Key quickly after pressing another, (for example, right then left). The character freezes for a moment, and then starts moving as it should. I tried using a method by senocular but it ended up making things more complicated even though it gave me fluid movement. Here's the original code. Could a Senior Member Help me out?!

Quote:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_xyscr extends MovieClip
{
var vx:int;
var vy:int;
var rightInnerBoundary:uint;
var leftInnerBoundary:uint;
var topInnerBoundary:uint;
var bottomInnerBoundary:uint;


public function Main_Character_xyscr()
{
init();
}
function init():void
{
//Initialize variables
vx = 0;
vy = 0;
rightInnerBoundary = (stage.stageWidth/2) + (stage.stageWidth/4);
leftInnerBoundary = (stage.stageWidth/2) - (stage.stageWidth/4);
topInnerBoundary = (stage.stageHeight/2) - (stage.stageHeight/4);
bottomInnerBoundary = (stage.stageHeight/2) + (stage.stageHeight/4);


//Add event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}

}


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


}
function onEnterFrame(event:Event):void
{
//Initialize local variables
var playerHalfWidth:uint = player.width / 2;
var playerHalfHeight:uint = player.height / 2;


//Move the player
player.x += vx;
player.y += vy;

//Stop player at inner boundary edges
if (player.x - playerHalfWidth < leftInnerBoundary-75)
{
player.x = leftInnerBoundary + playerHalfWidth-75;
background.x -=vx;
}
else if (player.x + playerHalfWidth > rightInnerBoundary+75)
{
player.x = rightInnerBoundary +75 - playerHalfWidth;
background.x -= vx;
}
if (player.y - playerHalfHeight < topInnerBoundary-25)
{
player.y = topInnerBoundary -25 + playerHalfHeight;
background.y -= vy;
}
else if (player.y + playerHalfHeight > bottomInnerBoundary+45)
{
player.y = bottomInnerBoundary +45 - playerHalfHeight;
background.y -= vy;
}

//Stop player at edges
if(player.x + playerHalfWidth> stage.stageWidth)
{
player.x = stage.stageWidth - playerHalfWidth + 5;
}
else if (player.x - playerHalfWidth < 0)
{
player.x = 0 + playerHalfWidth -3;
}
if (player.y - playerHalfHeight < 9)
{
player.y = 0 + playerHalfHeight + 9;
}
else if (player.y + playerHalfHeight > stage.stageHeight)
{
player.y = stage.stageHeight - playerHalfHeight+5;
}
}
}
}
Palifael is offline   Reply With Quote
Old 10-25-2011, 06:10 AM   #2
hmvince
Member
 
Join Date: Jul 2009
Posts: 83
Default

If I am correct, this will not happen if you press the up key quickly after the left key. The reason it does this is because you have vx for both left and right. Upon releasing the right key, vx is set to 0, so it cant be set to -5 when pressing left. make a different variable for each key:
leftMove
rightMove
upMove
downMove
This should fix your error. Please get back to me to see how it went!!!
hmvince is offline   Reply With Quote
Old 10-25-2011, 06:55 AM   #3
Palifael
Registered User
 
Join Date: Sep 2011
Posts: 10
Default

hmvince, Thank you! It worked great! I'd really like to consult with you again soon! Here's the working code:

Quote:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_xyscr extends MovieClip
{
var moveRight:int;
var moveLeft:int;
var moveUp:int;
var moveDown:int;
var rightInnerBoundary:uint;
var leftInnerBoundary:uint;
var topInnerBoundary:uint;
var bottomInnerBoundary:uint;


public function Main_Character_xyscr()
{
init();
}
function init():void
{
//Initialize variables
moveRight = 0;
moveLeft = 0;
moveUp = 0;
moveDown = 0;
rightInnerBoundary = (stage.stageWidth/2) + (stage.stageWidth/4);
leftInnerBoundary = (stage.stageWidth/2) - (stage.stageWidth/4);
topInnerBoundary = (stage.stageHeight/2) - (stage.stageHeight/4);
bottomInnerBoundary = (stage.stageHeight/2) + (stage.stageHeight/4);


//Add event listeners
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

}
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
moveLeft = 5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
moveRight = 5;
}
else if (event.keyCode == Keyboard.UP)
{
moveUp = 5;
}
else if (event.keyCode == Keyboard.DOWN)
{
moveDown = 5;
}

}


function onKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
moveLeft = 0;
}
if(event.keyCode == Keyboard.RIGHT)
{
moveRight = 0;
}
if (event.keyCode == Keyboard.UP)
{
moveUp = 0;
}
if (event.keyCode == Keyboard.DOWN)
{
moveDown = 0;
}


}
function onEnterFrame(event:Event):void
{
//Initialize local variables
var playerHalfWidth:uint = player.width / 2;
var playerHalfHeight:uint = player.height / 2;


//Move the player
player.x += moveRight - moveLeft;
player.y += moveDown - moveUp;

//Stop player at inner boundary edges
if (player.x - playerHalfWidth < leftInnerBoundary-75)
{
player.x = leftInnerBoundary + playerHalfWidth-75;
gamelevel.x -= moveRight - moveLeft;
}
else if (player.x + playerHalfWidth > rightInnerBoundary+75)
{
player.x = rightInnerBoundary +75 - playerHalfWidth;
gamelevel.x -= moveRight - moveLeft;
}
if (player.y - playerHalfHeight < topInnerBoundary-25)
{
player.y = topInnerBoundary -25 + playerHalfHeight;
gamelevel.y -= moveDown - moveUp;
}
else if (player.y + playerHalfHeight > bottomInnerBoundary+45)
{
player.y = bottomInnerBoundary +45 - playerHalfHeight;
gamelevel.y -= moveDown - moveUp;
}

//Stop player at edges
if(player.x + playerHalfWidth> stage.stageWidth)
{
player.x = stage.stageWidth - playerHalfWidth + 5;
}
else if (player.x - playerHalfWidth < 0)
{
player.x = 0 + playerHalfWidth -3;
}
if (player.y - playerHalfHeight < 9)
{
player.y = 0 + playerHalfHeight + 9;
}
else if (player.y + playerHalfHeight > stage.stageHeight)
{
player.y = stage.stageHeight - playerHalfHeight+5;
}
}
}
}
Palifael is offline   Reply With Quote
Old 10-25-2011, 06:56 AM   #4
hmvince
Member
 
Join Date: Jul 2009
Posts: 83
Default

YESssS!!!! THANKYOU! you are the first person on this forum that I have been able to give a good answer to that actually works! The moveRight - moveLeft thing was very clever. probably wouldnt have thought of that myself!
hmvince is offline   Reply With Quote
Old 10-25-2011, 06:40 PM   #5
Palifael
Registered User
 
Join Date: Sep 2011
Posts: 10
Default

Lol, I'm actually looking at a post you made about making an efficient flashgame, I guess I've been much more worried about getting things to move like I want to than finding out what the most efficient way is. I'm not using tiles at the moment, I'm using Movieclips with pixel animation, gif files in the flash timeline.
Palifael is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:51 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.