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.

Newcomers DBPro Corner / Random numbers and mouseclicks

Author
Message
Nemesis_0_
20
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Canada
Posted: 15th Nov 2007 06:13
so i was just wondering is there a way to make it so that when you click your mouse and have it generate only one random number? right now because its in a loop when I click, it generates one random number, does the one thing, but since its so fast it generates another and does the other thing... any way to tell it not to generate the next random number until the mouse has been let go, and pressed again???



Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Nov 2007 07:09 Edited at: 15th Nov 2007 07:13
you could deal with that atleast a couple different ways:

1) use a simple timer:



2) closer to what you asked for, use a flag to make sure mousebuttons have been released since last call :



make sense?

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
Nemesis_0_
20
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Canada
Posted: 15th Nov 2007 07:24 Edited at: 15th Nov 2007 07:50
never thought of that, but makes sense lol.. Thanks

[edit]

I tried implementing something like that but its not working. Essentially im trying to get it to place a bullet decal where you click on the launcher here is the function for it


what am i doing wrong here?

Jane Doe
17
Years of Service
User Offline
Joined: 29th Jun 2007
Location:
Posted: 15th Nov 2007 08:43
The problem is with your logic. Look at the following two lines:

Quote: "if mouseclick()=0 then released = 1
if mouseclick() and released = 1"


When the program hits the first line, the mouse button is either depressed or it isn't.

If the mouse button is depressed, then the first condition will evaluate to FALSE, and therefore the variable RELEASED won't be set to 1. This will prohibit the second condition in the second command from evaluating to TRUE and it won't work.

If the mouse button isn't depressed, the first condition will evaluate to TRUE and therefore the variable RELEASED will be set to 1. But since the button isn't depressed, the first condition of the second command will evaluate to FALSE and once again it won't work.

The only way that this can work is if the mouse button is depressed between the execution of these two commands. Good luck with that.

I think you need something like:



And don't forget to initialise the variable RELEASED to the proper value at the beginning of your programme.

- Jane
Flashing Blade
22
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 15th Nov 2007 09:29
The function will work ok if the variable released is made global.


The word "Gullible" cannot be found in any English Dictionary.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Nov 2007 10:05 Edited at: 15th Nov 2007 10:07
nemesis, try taking the flag check out of the function itself. try using it to either call or not call the function.

i think the problem you're experiencing now is that, and please someone correct me if i'm wrong, but i thought all variables within a function were accessable to that function only, unless defined as global?

i don't want to get this thread off on a tangent but it's been my (mis-?)understanding that functions work this way, re: variables local to that function alone. it's why i've NEVER used a function... can someone offer a yes or no to this?

@jane - it was poor practice for me to offer the example without initializing RELEASED = 0, but it's also my understanding that anything NOT intialized DOES = 0? true/not? not trying to be coy, just looking for a real answer.

also, did you try the 2 snippets? both worked appropriately for me before adding them to the post. the second is logical to me. or, did i just get lucky?

Quote: "
"if mouseclick()=0 then released = 1
if mouseclick() and released = 1"
"


the first line tells me "the mouse button was released"

the second tells me "the mouse buttons was clicked after it was released" which is to distinguish it from what could have been "the button is STILL being clicked" and/or "the button is being clicked", which isn't enough.

edit:

i finished typing my long-winded response here while you posted yours, flashing blade. i actually got a response BEFORE i posted a question. how can you ask for more?!

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
Nemesis_0_
20
Years of Service
User Offline
Joined: 26th Dec 2003
Location: Canada
Posted: 15th Nov 2007 15:57
HOORAY IT WORKS! Thank you all SO MUCH! XD

Jane Doe
17
Years of Service
User Offline
Joined: 29th Jun 2007
Location:
Posted: 15th Nov 2007 22:49
Hi Virtual Nomad,

Quote: "@jane - it was poor practice for me to offer the example without initializing RELEASED = 0, but it's also my understanding that anything NOT intialized DOES = 0? true/not? not trying to be coy, just looking for a real answer.

also, did you try the 2 snippets? both worked appropriately for me before adding them to the post. the second is logical to me. or, did i just get lucky?


Quote: "
"if mouseclick()=0 then released = 1
if mouseclick() and released = 1"
"

the first line tells me "the mouse button was released"

the second tells me "the mouse buttons was clicked after it was released" which is to distinguish it from what could have been "the button is STILL being clicked" and/or "the button is being clicked", which isn't enough."


The code that you initially posted - those two lines in particular - was embedded in a DO/LOOP - presumably the main programme loop. That means that we know that both of these lines of code will be executed constantly. Looking at it from that perspective, you're right. Those two lines of code can work independently of one another. The first line can be executed in one pass of the loop, then the second line can execute in the next pass.

Nemesis_0_'s implementation of that code however, was in the form of a function. Without contextual code to indicate when or how often this function is called, I just didn't want to assume that it was being called constantly and therefore the behaviour of the code might be different. His statement that it wasn't working suggested to me that this was the case.

That was the reason for my concern that the variable RELEASED is initialised to 1 (assuming the mouse button isn't being depressed at the beginning of the programme). Yes, you're right, a variable that isn't defined does evaluate to zero. I just didn't know when the first line would be executed and therefore when this would happen.


Nemesis_0_,

Quote: "HOORAY IT WORKS! Thank you all SO MUCH! XD"


Great!


- Jane

Login to post a reply

Server time is: 2024-11-25 12:47:35
Your offset time is: 2024-11-25 12:47:35