Keyboard event in as3 executed repeatedly

90 Views Asked by At

I'm new in AS3 language. I'd like to ask does KeyboardEvent in as3 used to executed as long as key is pressed? It gives me problem when performing a double jump for my character. Here's some of my code:

var dbJump:Boolean=true;
var onGround:Boolean=false;
var Yvel:Number=0;
var keyUp:Boolean=false;
this.addEventListener(Event.ENTER_FRAME,Eframe);
stage.addEventListener(KeyboardEvent.KEY_DOWN, 
kDown) 
stage.addEventListener(KeyboardEvent.KEY_UP, 
kUp) 
function kDown(e:KeyboardEvent):void{
    var key:uint=e.keyCode;
    if(key==32){
       keyUp=true;
    }
}
function kUp(e:KeyboardEvent):void{
    var key:uint=e.keyCode;
    if(key==32){
        keyUp=false;
    }
}
function Eframe(e:Event):void{
    Yvel++;
    myChar.y+=Yvel;
    while(ground.hitTestPoint(myChar.x,myChar.y,true)){
        myChar.y--;
        Yvel=0;
    }
    if(ground.hitTestPoint(myChar.x,myChar.y+3,true)){
        onGround=true;
        //if touching ground then return the dbJump to true;
        dbJump=true;
    else{
        onGround=false;
        if(!keyUp){
            //if not on ground an not pressed space, dbJump 
            // still true
            dbJump=true;
        }
    }
    if(keyUp && dbJump && onGround){
        Yvel=-12;
    }
    // if space pressed and not on the ground
    else if(keyUp && dbJump && !onGround){
        Yvel=-12;
        //then say doublejump is completed
        dbJump=false;
        trace(dbJump);
    }
}

so the dbJump variable immediately becomes false in the first space key press, not the second time space pressed. I put the trace so i can see how much is key down event repeated. So the point is the dbJump immediately became false as soon as my character off the ground. i didn't release the key yet. I tried to press and release the space keyboard quickly,and it works(so i'm really sure it is because the repeated statement of 'dbJump=false' . How to fix that? Also, i mean if the dbJump property is false it means cant jump again/doublejump have been completed.

0

There are 0 best solutions below