DirectButton : Increment number based on duration of press?

Say if the button is being held down then I can increment a number based on the during of it being down. As soon as the button is released, then stop incrementing the number.

Please can you let me know if this is possible?

Thanks in advance.

By “increment a number”, do you mean update an onscreen display with a constantly updating visible number? Or do you just mean increment an internal timer to keep track of how long the button is held down?

You can do either, but the easiest way to keep track of how long the button is held down is to record globalClock.getFrameTime() at the time the button is pressed, and subtract it from globalClock.getFrameTime() at the time the button is released.

If you want to do some fancier action, like update an onscreen counter, then you can spawn a task (with taskMgr.add()) when the button is pressed, and stop it (taskMgr.remove()) when the button is released.

You can use the button.bind() interface to bind a function on B2PRESS and B2RELEASE to do stuff like this on press and release.

David

It just occurred to me that by “button” you might be referring to a mouse button or a keyboard button, rather than to an onscreen DirectButton. If that’s the case, then rather than using button.bind(), you would simply accept() “mouse0” and “mouse0-up” events to track the button press and release events.

David

Thanks for the info David. I did indeed mean a gui button. I should have been a little more clearer. I have too many things floating around my head atm.

Ill give the bind() a go.

Thanks again.