A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Multiple Key.isDown issues on one MC animation

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    23

    Multiple Key.isDown issues on one MC animation

    I'm sure this has been asked a thousand times, but I can not find it in the search field (or I'm using the wrong verbage), so I thought I'd throw this, hopefully, simple question out there.

    I'm designing a simple platformer and everything works fine. Problem is, the actual MC animation. My jump and control functions are as follows:

    Actionscript Code:
    function jump () {
        if (hero.hitTest(ground)) {
            air = false;
        } else {
            air = true;
        }if (air == true) {
            hero._y += gravity;
            hero.gotoAndStop(3);
    //frame 3 is the jumping animation     
        } else {
            jumpcount = 0;
            jumpSpeed = 20;
        }if (jumpcount > maxjump) {
            jumpSpeed -= 1;
        }
    }
    ]function controls() {
        var KeyPressed = false;
        if (Key.isDown(Key.RIGHT)) {
            hero._x+=moveSpeed, hero._xscale = 100;
            hero.gotoAndStop(2);
    //frame 2 is the walking animation in hero MC
            KeyPressed = true
        } else if (Key.isDown(Key.LEFT)) {
            hero._x-=moveSpeed, hero._xscale = -100;
            hero.gotoAndStop(2);
    //frame 2 is the walking animation in hero MC
            KeyPressed = true;
        }  
        if (Key.isDown(Key.SPACE)) {
            hero._y -= jumpSpeed;
            jumpcount += 1;
            KeyPressed = true;
        }
       
       
       
        if (!KeyPressed) {
            hero.gotoAndStop(1);
        }

    Again everything works fine....except when TWO keys are used together. IE: When I walk left of right by itself, it plays the walking animation. Likewise, when I just jump, it plays the jumping animation. But if I press, say, right AND jump at the same time, my hero will jump up and to the right, but his animation stays locked on the 1st frame of the jump animation. You can really notice it when I jump straight up initially, then suddenly press a left or right mid-air...you watch the jump animation stop.

    I'm sure this is VERY simple, but I've tried several different things...even stuff like:
    Actionscript Code:
    if (Key.isDown(Key.SPACE) and Key.isDown(Key.LEFT)) {
            hero.gotoAndStop(3);

    in the controls function, but no luck.
    Last edited by bearsfan1978; 07-27-2011 at 09:36 PM.

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Add an isJumping boolean field that decides if you should use the run animation or leave it alone. When you jump set it true and when you land, set it false.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    23
    Sorry if I'm not understanding, but haven't I already done something like the isJumping statement with my air = true statement.

    If it's different, I'm not seeing how. Or where the isJumping portion would go in my example. Again, I could be missing something here, but not seeing how they would help play the jump animation if BOTH keys are being pressed. I'll keep chipping away at it, maybe something will give.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    It looks like you might be resetting the jump animation every frame the player is in the air. Pre-check the frame if air is true and only set the animation frame to 3 if the frame currently is not 3.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    23
    Unfortunately, I can't wrap my head around this. Even with that last suggestion. I've broken the code down even further to help test it, but still can't get nothing.
    Currently, I'm looking at
    Actionscript Code:
    this.onEnterframe = function(){
        var KeyPressed = false;
       
        if (Key.isDown(Key.RIGHT)) {
            hero._x+=moveSpeed, hero._xscale = 100;
            hero.gotoAndStop(2);
            KeyPressed = true;
                if (Key.isDown(Key.SHIFT)) {
                hero.gotoAndStop(3);
                KeyPressed = true;
                }
        }
        if (Key.isDown(Key.LEFT)) {
            hero._x-=moveSpeed, hero._xscale = -100;
            hero.gotoAndStop(2);
            KeyPressed = true;
                if (Key.isDown(Key.SHIFT)) {
                hero.gotoAndStop(3);
                KeyPressed = true;
                }
        }
       
        if (!KeyPressed) {
            hero.gotoAndStop(1);
        }
    }

    Very simple....press left or right...starts the animation in frame 2 while moving those directions. Press shift, though, and while it goes to the animation in frame 3 and moves left or right, it just sits there on the 1st frame of THAT animation. He basically slides left or right in that "stuck" first frame of that hero frame 3 animation. I let go of Shift and he continues left or right, but the normal frame 2 animation starts again. It's almost like we I have two inputs, it screws up the hero MC. Kinda like Flash gets confused and switches to the right animation, but forgets to let it play. But if I go back to one input, all is good. I don't get it.

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Looking at your original code, my last suggestion was to test the air variable and not change the frame:

    Code:
     if (Key.isDown(Key.RIGHT)) {
            hero._x+=moveSpeed, hero._xscale = 100; 
            if (!air) {
                    hero.gotoAndStop(2);
            }
    //frame 2 is the walking animation in hero MC
            KeyPressed = true;
    This would allow you to change directions mid-flight but not change the visible frame of the player.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center