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.
sync on
sync rate 60
autocam off
Key_Released = 1
phy start
make object box 1, 30, 2, 30
position camera 0, 20, -45
do
if spacekey() = 1 and Key_Released=1
Key_Released = 0
makeboxes()
endif
if spacekey() = 0
Key_Released=1
endif
sync
phy update
loop
function makeboxes()
for x = 2 to 102
make object box x, 1, 0.3, 0.5
position object x, RND(10)-5, x/2, RND(10)-5
color object x, RGB(RND(255), RND(255), RND(255))
next x
endfunction
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.
