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.

DarkBASIC Professional Discussion / return 0 even if the spacebar is pressed, sound wierd, but read the thread : )

Author
Message
mnemonic
19
Years of Service
User Offline
Joined: 14th Jan 2007
Location: Sweden
Posted: 30th May 2012 16:42
First,, a brief description of what I'm doing:

I have made a matrix terrain where I control a box using the arrowkeys to sweep over the terrain. When the user presses the spacebar the object will clone itself into that position.

Here is the code:



But!,,, I want the object to be cloned just once everytime the spacebar is pressed, but as long as the spacebar is depressed there will be a number of clones. There should be only one at the time.
How can I solve this? Do I need to use force feedback on the keyboard or is there another way?

www.memblockgames.com
MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 30th May 2012 16:56
Put wait 500 before the endif... If they keep it pressed then they will notice a alight lockup and then maybe multiple spawns... But it might help in a single hit...

Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 30th May 2012 17:31 Edited at: 30th May 2012 17:38
Well this works if it's not meant to be in real time...

if spacekey() = 1
clone object cloneObject,2
inc cloneObject,1

repeat
Until spacekey() = 0
endif



If it does have to be in real time...

if spacekey() = 1 and placed = 0
clone object cloneObject,2
inc cloneObject,1
Placed = 1

endif


If spacekey() = 0
placed = 0
Endif




And if you want a timer you can also put

if spacekey() = 1 and placed = 0
clone object cloneObject,2
inc cloneObject,1
Placed = 1
t = timer()
endif
If t <= timer() - 1000 then placed = 0


To drop them every second.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th May 2012 20:57
It's very easy to do properly: what you want to do is check if the spacekey is pressed this frame but wasn't pressed last frame. You can do that like so:



[b]
mnemonic
19
Years of Service
User Offline
Joined: 14th Jan 2007
Location: Sweden
Posted: 31st May 2012 18:51
Thank you all for your advice, it helped! Thanks!

www.memblockgames.com
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st May 2012 19:47 Edited at: 31st May 2012 20:22
You can use the key to handle the variables for you:


Make it even neater by storing the comparison in a variable:


This does the same as diggsey's example but makes use of the behaviour of the key. We want to run the code only once every time the key is pressed, so we are checking for the first key-press: the time the keystate changed from 0 to 1. Since we are looking for a change we must store the current keystate for comparison in the next iteration. We compare the old keystate with the new, and if the new is greater this must be the first key-press (since the only possible values are 0 and 1). Old takes the value of new, so while the key is held the old value will also be 1 on subsequent iterations, so new is not greater than old and the code doesn't execute. When the key is released we have the first release, where new=0 and old=1 (we could make something else happen on this event if we liked {if old>new}) then old takes the value 0 from new and we're back to the default state.

Login to post a reply

Server time is: 2026-07-09 02:35:43
Your offset time is: 2026-07-09 02:35:43