this is my code. whenever i'm pressing down on left arrow key and when i press the spacebar it stops and jumps and it stops going to the left anymore(same with the right arrow key).
stop();
var Gravity = 5;
var yLimit = 400 - char.height;
var friction = .9;
var velocity = 100;
var djctr = 0;
stage.addEventListener(Event.ENTER_FRAME, entFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, control);
function control(event:KeyboardEvent){
if(char.y >= yLimit || djctr == 2){
if(event.keyCode == 32 && djctr !=2){
char.y -= velocity;
char.x += 7;
djctr+=2;
}else if (event.keyCode == 32 && djctr == 2){
char.y -= velocity * .50;
djctr+=2;
}
}
if(event.keyCode == 37){
char.x -= 7;
}else if(event.keyCode == 39){
char.x += 7;
}
}
function entFrame(e:Event){
char.y += Gravity;
if(char.y >= yLimit){
char.y = 400-char.height;
djctr = 0;
}
}
What I do for movement control is by having two stage KeyboardEvent listeners (KEY_DOWN and KEY_UP), and a Timer that runs every frame. The keyboard events don't handle control, they only handle changing static values such as
keyLeftIsDownandkeyRightIsDown(in your case you can also handle when the spacebar is pressed), and the Timer listener is what handles control. This is what it might look like:This way the character moves in the x direction even if the code is handling jumps!