Computer Science Canada Making a button press occur only once. |
Author: | Reality Check [ Tue May 09, 2006 9:18 pm ] | ||
Post subject: | Making a button press occur only once. | ||
Alright I'm doing this little thing for school. Heres the code before I go further.
Now, on the top left corner of my screen is the bill. This is basically a computerized bill making machine for a play. Families come to a play and get the bill. The only 2 buttons that work right now are the children one and the student one but I didn't go further because of a problem I encountered. As you see I add the specified price to the variable 'bill' each time the mouse is clicked in the specified area. Now, take a look at the top left corner where I put the bill. It goes up by a lot more than just the specified price. I know why (because you can't click fast enough to make it true only once) How could I fix this? |
Author: | Martin [ Tue May 09, 2006 9:35 pm ] | ||
Post subject: | |||
It's like a machine gun. You click and hold, and bullets just keep coming out. What you want is it to be like a pistol. You click, a bullet comes out, but you have to stop clicking and click again to shoot again. So what you have to do is have a switch, like so (in pseudocode)
It works like this. has_clicked is false. The user clicks on the button. has_clicked is false, so the button's action happens. The next iteration of the loop, the button's still down, but has_clicked is true, so we know that we've already done the action. As soon as they lift up the button, has_clicked becomes false again and the user is free to click once more. Cool? |
Author: | Reality Check [ Tue May 09, 2006 9:40 pm ] |
Post subject: | |
Wow man thanks a lot. I'd give you bits if I knew how... |
Author: | Martin [ Tue May 09, 2006 9:41 pm ] | ||
Post subject: | |||
One thing you might want to do is make has_clicked become true if the button is clicked, regardless of if it's over the button. With the code I provided, clicking and dragging the mouse over the button will cause the button to click. So instead, try something like this:
|
Author: | Reality Check [ Tue May 09, 2006 9:48 pm ] |
Post subject: | |
^^Yea I was meaning to think of a way to do that too...you learn something new everyday! |
Author: | Martin [ Tue May 09, 2006 10:01 pm ] |
Post subject: | |
Means it was a well spent day. I'm glad to be a help. ![]() |
Author: | Zacebdal [ Tue Oct 17, 2006 8:15 pm ] | ||
Post subject: | Solution | ||
For anyone else that is looking for an answer to this problem(i know i was). I just started using this a while ago but have figured a rather easy way to fix it. It has worked up to this point for me and it may not be the best but its what ive been using.
This is just an example program. Just use the skeleton to make your own. Hope this helps, its a newbie approach but its kinda simple. ![]() |