Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Health Mathematics..

Author
Message
Reflux
15
Years of Service
User Offline
Joined: 25th Feb 2009
Location:
Posted: 16th Mar 2009 11:23
Hey guys.. Im trying to something real simple its just bugging me.. I have an object.. and when the object is hit.. I want it to reduce its health from 100. Heres what i have so far,



Probably really simple.. But when I hit the [2] key.. and hold it.. its health continually decreases.. I only want it to reduce the health by 0.12 per each keypress... no matter if you hold the key down or not. Thanks in advance
Bruce Dudeson
15
Years of Service
User Offline
Joined: 17th Mar 2009
Location:
Posted: 17th Mar 2009 12:34
What your doing is checking to see if key[2] is being pressed. However, you only want to check if it was triggered. In order to do this you have to keep track of the keys status. your going to need 2 variables keyCurrent and keyOld. During your loop you'll want to update the keys, setting keyOld to keyCurrent and keyCurrent to 0;



now check for key[2]


with that you can check if a key is pressed or if its triggered like so




Now your code might look like this.


Though that could be cleaned up a lot. I recommend making an input manager. It will have a list of keys to keep track of. It will have functions like getInput, updateKeys, isPressed, isTriggered.
entomophobiac
21
Years of Service
User Offline
Joined: 1st Nov 2002
Location: United States
Posted: 17th Mar 2009 13:37 Edited at: 17th Mar 2009 13:39
In that case, you have to write code that identifies whether the key is held or hit. The code you have there will be true for as long as the button is held.

You could reformat it as such:




Didn't try this out or anything and I'd suggest you write a better implementation of the same functionality, such as an Input-class or similar, but I guess it should do the job, at least...
Reflux
15
Years of Service
User Offline
Joined: 25th Feb 2009
Location:
Posted: 18th Mar 2009 13:07
Thanks guys.. Both worked.. But entomophobiac's way shortens it alot.. less variables. Thanks guys. Greatly Appreciated.
Bruce Dudeson
15
Years of Service
User Offline
Joined: 17th Mar 2009
Location:
Posted: 19th Mar 2009 07:38
No problem Though mine was more aimed at starting to make an input manager rather than a quick fix.

Login to post a reply

Server time is: 2024-09-30 21:29:53
Your offset time is: 2024-09-30 21:29:53