0

First, I know, I'm still using AS 2, I should get with the times, but for now I'm using AS2. I had this programmed on a different game, but for some reason, I can't duplicate the result, and I accidentally deleted the other game. My problem is I am trying to make the player do 3 different attacks, one for each time you hit the "A" key, but all it does is the first attack.

Here's my set up.

I have a movie clip with 4 keyframes inside it, one keyframe has an idle player movie clip labeled "idle", one with a walking movie clip labeled "walking", one with a jumping player movie clip labeled "jumping, and finally, one for attacking which, you guessed it, is labeled "attack". In my attack movie clip, i have the animation for 3 attacks, with the last frame of each attack having a stop command, followed by a check if the key "A" is pushed, and if it is pushed, then it plays again. This is repeated for each attack.

In my main player movie clip (the one containing all the others) I have the following code written.

 onClipEvent(enterFrame){
 var walkspd = 5;
 var sprintspd = 2;
 var gravity = 5;   
 var decel = .1;

//walking

if (Key.isDown(Key.RIGHT)){
    this.gotoAndStop("walk");
    this._xscale = 100
    this._x += walkspd;
}
if (Key.isDown(Key.LEFT)){
    this.gotoAndStop("walk");
    _xscale = -100;
    _x -= walkspd;
}
else {
    if(Key.isDown(Key.RIGHT) == false && Key.isDown(Key.LEFT) == false && Key.isDown(65) == false){
        this.gotoAndStop("idle");
    }
}

//attacking

if (Key.isDown(65)){
    gotoAndStop("attack")
}

}

How do I get this to work properly?

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • If you find the answer useful please accept it by clicking on the check mark "✅" icon. – Amer Oct 23 '13 at 09:51

1 Answers1

0

If I understand you correctly, you need to show a new attack clip (1 of 3) on each press of ‘A’ key… If that is your situation, please look at this working example (it is CS5 .fla file, sorry I can’t save it in an older format since I only have the latest version installed):

http://www.4shared.com/zip/Z-Adpv_x/KeyMoveExample.html

The fixed version of your code should looks like this:

onClipEvent (load) {
    // Constants :
    var walkspd:Number = 5;
    var sprintspd:Number = 2;
    var gravity:Number = 5;
    var decel:Number = .1;
    // Move Keys :
    var IsLeftKeydown:Boolean = false;
    var IsRightKeydown:Boolean = false;
    var IsAKeydown:Boolean = false;
    // Attack movie :
    var totalAttacks:Number = 3;
    var currentAttack:Number = 1;
    // create the keyListener Object : 
    var keyListener:Object = new Object();
    keyListener.onKeyUp = function()
    {
        //trace("UP -> Code: " + Key.getCode() + "\tASCII: " + Key.getAscii() + "\tKey: " + chr(Key.getAscii()));

        // detect when the 'A' key is released to increase current action :
        if (Key.getCode() == 65)
        {
            currentAttack++;
            if (currentAttack > totalAttacks)
            {
                currentAttack = 1;
            }
        }
    };
    Key.addListener(keyListener);
}
// --------------
onClipEvent (enterFrame) {

    IsLeftKeydown = Key.isDown(Key.LEFT);
    IsRightKeydown = Key.isDown(Key.RIGHT);
    IsAKeydown = Key.isDown(65);

    //attacking
    if (IsAKeydown)
    {
        gotoAndStop("attack");
        attack_mc.gotoAndStop(currentAttack);
    }
    else
    {
        //walking
        if (IsRightKeydown)
        {
            gotoAndStop("walk");
            _xscale = 100;
            _x += walkspd;
        }
        else if (IsLeftKeydown)
        {
            gotoAndStop("walk");
            _xscale = -100;
            _x -= walkspd;
        }
        //idle
        else
        {
            gotoAndStop("idle");
        }
    }
}

note: I assume the name of your attack movie is 'attack_mc', change it in the previous code to suite your needs.

hope that help you a little.

Amer
  • 468
  • 1
  • 5
  • 15