A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: disable key

  1. #1
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678

    disable key

    Is there a way to disable a keyboard key after you press.
    As in, if you press and hold, it will only execute the action for that key once.

    on (press, keyPress "<Left>") {
    \\somehow disable the key
    this.gotoAndPLay("walk");
    }

    Thanks for your help.

  2. #2
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Code:
    on (press, keyPress "") {
     if (disabled != true) {
      this.gotoAndPLay("walk");
      disabled = true;
     }
    }
    on (release, keyRelase) {
     disabled = false;
    }
    I might have made up the 'keyRelease' handler ... you'd need to check it out. But that should work, in theory.
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  3. #3
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678
    Thanks, that works.
    How do you enable the key (and play a certain frame) when you release the key?
    The keyRelease does not work.
    Thanks.

  4. #4
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Okay ... take the code off the button, make the button into a movieClip, then put this code on the movieClip:
    Code:
    onClipEvent (load) {
     disabled = false;
    }
    onClipEvent (mouseDown) {
     if (this.hitTest(_root._xmouse, _root._ymouse, true && disabled != true) {
      this.gotoAndPLay("walk");
      disabled = true;
     }
    }
    onClipEvent (keyDown) {
     if (disabled != true) {
      this.gotoAndPLay("walk");
      disabled = true;
     }
    }
    onClipEvent (mouseUp) {
     disabled = false;
    }
    onClipEvent (keyUp, mouseUp) {
     disabled = false;
    }
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  5. #5
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678
    there are syntax errors that i dont know how to fix.
    i fixed the one on line 5, but can't figure out what's wrong on line 19, it says there is a unexpected ")", but i can't find it.

  6. #6
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Code:
    onClipEvent (load) {
     disabled = false;
    }
    onClipEvent (mouseDown) {
     if (this.hitTest(_root._xmouse, _root._ymouse, true) && disabled != true) {
      this.gotoAndPlay("walk");
      disabled = true;
     }
    }
    onClipEvent (keyDown) {
     if (disabled != true) {
      this.gotoAndPlay("walk");
      disabled = true;
     }
    }
    onClipEvent (mouseUp) {
     disabled = false;
    }
    onClipEvent (keyUp) {
     disabled = false;
    }
    Apologies ... try that.
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  7. #7
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678
    Thanks for the code.
    is there a way to specify a key for the KeyDown and keyUp functions, that will still work with the code?

  8. #8
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Yes, but what had you in mind? It's difficult to code without knowing exactly what you wanted.
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  9. #9
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678
    All i'm trying to do is to make a guy walk forward, but when you hold down the key, it keeps in going back to the beginning frame of the movie clip without playing it all the way through, so i thought disabling the key would be the best way to do that, and when you release the key, it would enable it again. But if there is a easier way to make the guy walk, please tell me. Thanks.

  10. #10
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Yes, there is a SLIGHTLY easier way ...
    Code:
    onClipEvent (enterFrame) {
     if (Key.isDown(Key.LEFT) && this._currentframe < this._totalframes) {
      nextFrame();
     }
    }
    Put that code on the movieClip of the 'walking' action, and when you press the left arrow key on the keyboard it will start walking. Let the key go and it will stop.
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  11. #11
    I tried... dbarbarian's Avatar
    Join Date
    Aug 2003
    Location
    Berkeley, California, USA
    Posts
    678
    I should have probably said that the walk movie clip is inside another movie clip. Sry about that.
    Would that affect the code even if i changed the onClipEvent to the "load".

  12. #12
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Put the code I posted above ON THE CLIP CONTAINING THE ANIMATION. Doesn't matter what else is going on around it ... just make sure that code is on the actual movieClip.
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

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