PDA

View Full Version : [AS3] Help with game sprite movement


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

henke37
10-05-2009, 06:33 AM
Get used to it, the listener will fire more than once due to keyboard repetition. It can not be disabled.
What you have a problem with is however not caused by this. It is caused by the continious gotoAndPlay calls. Only do those once, when changing the animation.

Personaly, I would use a setter for this and keep track of the current state, only calling gotoAndPlay when it changes.

PrinceOfPersia
10-05-2009, 07:10 AM
I think i got it. Basically i need to create a new variable that holds the status, and then make the function that checks the status, only once to make the "gotoAndPlay"?