PrinceOfPersia
10-05-2009, 01:10 AM
Hi, i have a game project, im having problems with character movement. the character is Megaman(yes, the blue guy). i have som variables and listener so when i press the arrow keys, the screen should move to simulate movement of the character, and it plays a loop of frames of a movie clip, running left or right depending on the key pressed. But it seems when i press a key, it keeps listening the "pressed" continuosly, so the megaman clip stays on the first frame but the stage moves, when i release the key, the megaman stars running normaly, but the stage doesnt move. What im doing wrong? It should start running and the stage should move when i press a key, and then stop when i realease. how i make the frames play while i keep pressed the keys?
stop();
var keyPressed:uint;
var spaceIsDown:Boolean;
var rightKeyIsDown:Boolean;
var leftKeyIsDown:Boolean;
var upKeyIsDown:Boolean;
var downKeyIsDown:Boolean;
function initializeGame():void
{
spaceIsDown = false;
rightKeyIsDown = false;
leftKeyIsDown = false;
upKeyIsDown = false;
downKeyIsDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
}
initializeGame();
function pressKey(event:KeyboardEvent):void
{
keyPressed = event.keyCode;
if (keyPressed == Keyboard.UP)
{
trace("se presionó tecla arriba");
upKeyIsDown = true;
}
if (keyPressed == Keyboard.DOWN)
{
trace("se presionó tecla abajo");
downKeyIsDown = true;
}
if (keyPressed == Keyboard.LEFT)
{
trace("Se presionó tecla izquierda");
leftKeyIsDown = true;
}
if (keyPressed == Keyboard.RIGHT)
{
trace("Se presionó tecla derecha");
rightKeyIsDown = true;
}
megaman_mc.addEventListener(Event.ENTER_FRAME, moveMegaman);
}
function releaseKey(event:KeyboardEvent):void
{
var thisKey:Number = event.keyCode;
if (thisKey == Keyboard.UP)
{
upKeyIsDown = false;
}
if (thisKey == Keyboard.DOWN)
{
downKeyIsDown = false;
}
if (thisKey == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if (thisKey == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
spaceIsDown = false;
}
function moveMegaman(event:Event):void
{
if(rightKeyIsDown == true)
{
megaman_mc.gotoAndPlay("correrDerecha");
bkg_mc.x -=2;
}
if(leftKeyIsDown == true)
{
megaman_mc.gotoAndPlay("correrIzquierda");
bkg_mc.x +=2;
}
if(downKeyIsDown == true)
{
megaman_mc.gotoAndStop("stopDerecha");
}
stop();
var keyPressed:uint;
var spaceIsDown:Boolean;
var rightKeyIsDown:Boolean;
var leftKeyIsDown:Boolean;
var upKeyIsDown:Boolean;
var downKeyIsDown:Boolean;
function initializeGame():void
{
spaceIsDown = false;
rightKeyIsDown = false;
leftKeyIsDown = false;
upKeyIsDown = false;
downKeyIsDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
}
initializeGame();
function pressKey(event:KeyboardEvent):void
{
keyPressed = event.keyCode;
if (keyPressed == Keyboard.UP)
{
trace("se presionó tecla arriba");
upKeyIsDown = true;
}
if (keyPressed == Keyboard.DOWN)
{
trace("se presionó tecla abajo");
downKeyIsDown = true;
}
if (keyPressed == Keyboard.LEFT)
{
trace("Se presionó tecla izquierda");
leftKeyIsDown = true;
}
if (keyPressed == Keyboard.RIGHT)
{
trace("Se presionó tecla derecha");
rightKeyIsDown = true;
}
megaman_mc.addEventListener(Event.ENTER_FRAME, moveMegaman);
}
function releaseKey(event:KeyboardEvent):void
{
var thisKey:Number = event.keyCode;
if (thisKey == Keyboard.UP)
{
upKeyIsDown = false;
}
if (thisKey == Keyboard.DOWN)
{
downKeyIsDown = false;
}
if (thisKey == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if (thisKey == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
spaceIsDown = false;
}
function moveMegaman(event:Event):void
{
if(rightKeyIsDown == true)
{
megaman_mc.gotoAndPlay("correrDerecha");
bkg_mc.x -=2;
}
if(leftKeyIsDown == true)
{
megaman_mc.gotoAndPlay("correrIzquierda");
bkg_mc.x +=2;
}
if(downKeyIsDown == true)
{
megaman_mc.gotoAndStop("stopDerecha");
}