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 / Multiple keypresses

Author
Message
3than
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location:
Posted: 1st Oct 2011 07:39
O.k, I'm using DarkBASICPro with dark physics right now. Here's my code:



Here's an example if you don't have Dark Physics:



Compile and run one of them and press the space key.
Since the sync rate is 60, it'll check if the user has pressed the space key or not 60 times a second. if it detects that you've pressed it more than once in a 60th of a second (which is kind of impossible to avoid), it'll tell you that the object on line 20 already exists.
I just want to ask if there's any way you could make it check for the space key once a second without changing the sync rate.

or, even better, if there's a way to make more cubes fall without having to change the ID number each time(if you get what I mean).
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 1st Oct 2011 12:00
Yeah, like this...



zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 13:32 Edited at: 1st Oct 2011 14:16
My personal method for dealing with situations like this differs from Pincho's solution, and actually it avoids the actual question you asked. Whether or not it will work for you greatly depends on what 'really' needs to be controlled. Is it limiting the checks to a specific time (once every second)? Or is it preventing multiple calls to the function when the end user is actually attempting to make a single selection?? If its the former then Pincho's answer would be best,, if it's actually the latter phrasing then the following may be what you are looking for.

Let me explain.

Rather than limiting how often the program checks for a key being pressed... I simply make sure that the key is only checked once when the key is HELD down, and it won't runt through that code again until the key is RELEASED and then held down again. TO do this I use a 'flag variable' (a.k.a. A boolean, or make it an integer if your prefer, as DarkBasic seems to allow booleans to hold values from 0 to 256 (8-bits) rather than limiting them to 1 or 0 (1 bit) like some languages). blah, blah, blah

So have a variable named Key_Released. This variable is equal to 0 if the <spacebar> is down/held, and it is equals 1 if the <spacebar> is released. Now you just need to add that check into your code.



The above relieves the situation of making multiple calls to makeboxes() when the user is attempting to make a single selection.


I don't have Dark Physics so excuse me if I am not in the know of how it handles objects. I don't see much of a difference in your sample code between Dark Physics and No Dark Physics; except for the line (phy make rigid body static box 1)... but phy start & phy update would error. That said...

*** NOTE *** I will have to run the code to test... but, I can't see how either solution deals with the issue that on the "next" successive call to makeboxes (whether that "next" call is after 1 second OR after the key is released); you would still be creating objects that already exist. Namely object numbers 2-102. U

nless I am missing something here. I would think that you would need to manage your objects numbers by either...

- Keeping track of existing objects, deleting uneeded objects, and reutilizing only the object numbers that have been deleted
- Always create new objects numbers (Not really a viable option)

OR

- Limit the call to makeboxes() only happen either once - or after All created objects are destroyed!
Hmmmmmm?????

Your signature has been erased by a mod please reduce it to 600 x 120.
3than
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location:
Posted: 4th Oct 2011 12:13
Hey, that worked! Thanks zenassem. I'll remember that for my other programs I make
zenassem
23
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 4th Oct 2011 17:44 Edited at: 4th Oct 2011 17:49
Your welcome 3than.

Btw I should mention that since the states of the keypresses will be 1 or 0 you could eliminate the "=1" and "=0" (in the IF conditions) and replace them with a logical expression.



Your signature has been erased by a mod please reduce it to 600 x 120.
Santman
14
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 5th Oct 2011 03:04
There's another way that I use, and that's a simple repeat loop but I use keystate's to check. By checking for the keystate to match the spacebar (it's 57), then start an empty "repeat until" loop that checks that the keystate DOES NOT EQUAL the space bar:

if keystate(57)=1
repeat until keystate(57)=0
endif

This will prevent the code from moving on until the relevant key has stopped being pressed. A simple counter could be added (zero'd at the start) using an OR so that if it's still being pressed after a certain number of cycles the code continues anyway:

if keystate(57)=1
count=0
repeat
inc count
until keystate(57)=0 or count>100
endif

Useful for, say, joystick controls on a menu screen, or checking any other key you want really.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th Oct 2011 09:08
The quick way is to check for all keys at the same time using SCANCODE() so there's no need to check individual keys after every use.



baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 7th Oct 2011 15:10
I wouldn't name too many threads like this or you'll get slapped for unhelpful thread naming. Just a friendly warning...

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Oct 2011 18:44 Edited at: 7th Oct 2011 18:44
Thread title changed

Unhelpful Titles

Please do not simply write "Noob Question", "I am a newb", "One Noobie Question", "A question", "Please help", "This isn't working" or anything similar as the title of your post. We have a search facility, and meaningless titles do not help others find answers to their questions. It will also help you to get quicker responses if people have an idea of your thread content.

Login to post a reply

Server time is: 2026-07-10 04:20:53
Your offset time is: 2026-07-10 04:20:53