dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: How to detect how long a key has been pressed for?

  1. #1
    (sic) Covenent's Avatar
    Join Date
    Dec 2000
    Location
    Ireland
    Posts
    709
    Without using any long about ways involving timing it with frames.

    I have tried using methods like .getSeconds and things but I just cant get it right.

    Cany anyone help me out with some code?

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    try getTimer instead...
    getSeconds is a bit crude for most purposes (you could get
    milliseconds instead) but both wrap around so you would have to deal with end time < start time situations

    Musicman

  3. #3
    (sic) Covenent's Avatar
    Join Date
    Dec 2000
    Location
    Ireland
    Posts
    709
    Sorry but could you type out some code for me, just something basic that would lower a movie clips alpha by 5 after every 2 seconds.

    I have tried using .getMilliseconds but I havent got very far with it.

    All help is appreciated,
    Cheers

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    I thought you were trying to measure time people hold their buttons down...
    The effect you are looking for
    when the button gets pressed:
    counter = 0
    active = 1
    when the button gets released:
    active = 0
    in the MC
    Code:
    onclipevent(enterframe)
    {	if(active)
    	{	++counter;
    		if(counter % 24 == 0 && this.alpha > 0)
    			this._alpha -= 5;
    	}
    }
    If the button is not inside the movieclip, make sure you target variables so the button and the mc dont miss each other's variables
    24 means 2 seconds at 12 fps

    Musicman

  5. #5
    Senior Member Leo Lima's Avatar
    Join Date
    Jul 2000
    Location
    São Paulo, Brazil
    Posts
    745
    You can try, adding to Musicman's answer:
    Code:
    onClipEvent (keyDown) {
    	if (!active) {
    		active = true;
    		start = getTimer();
    		x = 2000;
    		// 2 seconds
    	}
    	updateAfterEvent();
    }
    onClipEvent (keyUp) {
    	active = false;
    	updateAfterEvent();
    }
    onClipEvent (enterFrame) {
    	if (active) {
    		if (getTimer()-start>x) {
    			start = getTimer();
    			// action to perform every X milisseconds
    			this._alpha -= 5;
    		}
    	}
    }
    This works on MCs, and is specifically to your problem. (Fade away every 2 seconds of keyPress)

    Regards,
    Leo Lima

  6. #6
    (sic) Covenent's Avatar
    Join Date
    Dec 2000
    Location
    Ireland
    Posts
    709

    Forget about my previous post

    No sorry I obivously didnt make myself clear. I know how to decrease the property of a movie clip by using onClipEvent (enterFrame)

    What I am looking for is a way to measure how long a person pressed, lets say the spacebar. So I would a movieclip that would contain a dynamic text with a name of 'result'.
    'result' would start off with a value of 0.
    Now when the user presses the spacebar a counter starts, and when they let go the counter stops; then 'results' is equal to the length of time the spacebar was held down for.
    I know that the basics of the script would be like this.

    onClipEvent (load) {
    this.results=0;
    }
    onClipEvent (keydown) {
    if (Key.isDown(Key.SPACE)) {
    //start timer
    }
    }
    onClipEvent (keyup) {
    //stop timer
    results=timer;
    }

    Im just not too sure on how to work the timer bit.


  7. #7
    (sic) Covenent's Avatar
    Join Date
    Dec 2000
    Location
    Ireland
    Posts
    709
    Thanks Leo that seems to be exactly what I want, cheers for the help guys.

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