A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Key.isDown HELP

  1. #1
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874

    Key.isDown HELP

    I have the following code as a pause button as my game is run threw a timer...
    Code:
    onClipEvent (enterFrame) {
    	_root.frame = _root.timer._currentframe;
    	if ((Key.isDown(Key.SPACE)) and (_root.pauser._currentframe == 1)) {
    		_root.timer.gotoAndStop(_root.frame);
    		_root.pauser.gotoAndStop(2);
    	} else if ((Key.isDown(Key.SPACE)) and (_root.pauser._currentframe == 2)) {
    		_root.timer.gotoAndPlay(_root.frame);
    		_root.pauser.gotoAndStop(1);
    	}
    }
    When i press space the pause MC appears and then disperes. I know why this happens but i cant figure out a way around it...
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  2. #2
    Member
    Join Date
    Sep 2004
    Posts
    40
    it happens because Key.isDown returns true if the key is down, not when it's pressed. the game is probably going through more than one frame in which the spacebar is down, because you can't press it and let up fast enough. here's my solution:

    Code:
    spaceDown = false;
    onClipEvent (enterFrame) {
       _root.frame = _root.timer._currentframe;
       if (Key.isDown(Key.SPACE)){
          if (_root.pauser._currentframe == 1 && !spaceDown){
             _root.timer.gotoAndStop(_root.frame);
             _root.pauser.gotoAndStop(2);
          } else if (_root.pauser._currentframe == 2 && !spaceDown){
             _root.timer.gotoAndPlay(_root.frame);
             _root.pauser.gotoAndStop(1);
          }
          spaceDown = true;
       } else {
          spaceDown = false;
       }
    }
    this is if you want to keep everything in an onEnterFrame function. tell me if that helps.

    EDIT: sorry, i made a mistake, the spaceDown variable needs to be defined outside the onEnterFrame function or else it'll be reset every frame. It's fixed now.
    Last edited by generaleggplant; 11-03-2005 at 04:04 AM.

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