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;
}
}
}
}
|