Crossman
Crossman

Reputation: 278

Single Action with Button Click

I am currently building a small version of a fighting game in flash, but I don't want the character to continuously hit etc. by just holding down a key, but rather to have the key be pressed everytime. This is what I currently have:

function moveChar(event:Event):void{
if(rightKeyDown && !hitting && !combo){
    hitting = true;
    gotoAndPlay("basic_punch");
    kickbag.gotoAndPlay("hit1");
    countHits++;
}
if(downKeyDown && !hitting && !combo){
    hitting = true;
    gotoAndPlay("basic_kick");
    kickbag.gotoAndPlay("hit1");
    countHits++;
}
if(downKeyDown && combo){
    gotoAndPlay("kick_combo1");
    kickbag.gotoAndPlay("hit2");
    kickbag.stop();
}
if(rightKeyDown && combo){
    gotoAndPlay("punch_combo");
    kickbag.gotoAndPlay("hit2");
    kickbag.stop();
}
}

function checkKeysDown(event:KeyboardEvent):void{
    if(countHits == 2) bar.gotoAndStop("bar2");
    if(countHits == 6) bar.gotoAndStop("bar3");
    if(countHits == 10) {
        bar.gotoAndStop("bar4");
        combo = true;
        gotoAndPlay("combo_stand");
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
    }
    else if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = true;
    }
}

function checkKeysUp(event:KeyboardEvent):void{
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = false;
    }
    else if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = false;
    }
}

But as I explained this allows for the buttons to be held down.

Upvotes: 0

Views: 59

Answers (3)

Bennett Keller
Bennett Keller

Reputation: 1714

I think @Ribs is on the right track, but given your current code the answer seems simple.

Remove the Event.ENTER_FRAME from moveChar() and change the function definition line so it looks like this:

function moveChar():void //e:Event removed from parameters

Then in your checkKeysDown function, call the moveChar() for the key pressed (your moveChar() evaluates the booleans anyway):

if(event.keyCode == 39 || event.keyCode == 68){
    rightKeyDown = true;
    moveChar();
}
else if(event.keyCode == 40 || event.keyCode == 83){
    downKeyDown = true;
    moveChar();
}

Now you can alter the if statements above to make it a bit more concrete:

if ( !downKeyDown && !rightKeyDown ) {
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
        moveChar();
    }
    else if(!downKeyDown && !rightKeyDown && (event.keyCode == 40 || event.keyCode == 83)){
        downKeyDown = true;
        moveChar();
    }
}

This is basically just handling the down or right key pressed one at a time, and ignoring everything else while those are running. It could also be assume that you might not want to do anything while down or right keys are pressed, therefore you can just do this:

function checkKeysDown(event:KeyboardEvent):void{
    if ( downKeyDown || rightKeyDown ) return; //do nothing, we are pressing a key
    if(countHits == 2) bar.gotoAndStop("bar2");
    if(countHits == 6) bar.gotoAndStop("bar3");*/
    if(countHits == 10) {
        bar.gotoAndStop("bar4");
        combo = true;
        gotoAndPlay("combo_stand");
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
        moveChar();
    }
    else if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = true;
        moveChar();
    }
}

This will return in the checkKeyDown function if we are currently pressing the down or right key. I'm assuming this is probably the desired effect you are going for so your counters do not increment each KEY_DOWN registration. As you can see you have some options and there are multiple ways of doing this. Hope this provides some guidance.

Upvotes: 0

DrakeTruber
DrakeTruber

Reputation: 337

I too have been working on a platformer flash game, and here's how I accomplished a clean attack code: Declare an "attack" Boolean to help decide whether or not the character can or cannot attack. You can prevent the character from continuously attacking by creating an enter frame event listener that checks what frame the character is in during his attack. If the character play-head is at the last frame of the animation than simply set the attack Boolean to false.

Upvotes: 0

Ribs
Ribs

Reputation: 1505

Create onKeyDown and onKeyUp listeners. In the onKeyDown listener kill the onKeyDown listener, perform action, and init the onKeyUpListener. In the onKeyUp listener, kill the onKeyUp listener and re-init the onKeyDown listener.

Upvotes: 1

Related Questions