Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Making a button press occur only once.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Reality Check




PostPosted: 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.

code:
var x, y, b : int
var font : int := Font.New ("Palatino:24:bold,italic")
setscreen ("offscreenonly")
var bill : real := 0

loop
    View.Update
    locate (1, 1)
    put bill
    Mouse.Where (x, y, b)
    drawfillbox (25, 90, 500, 15, brightred)
    Font.Draw ("Children Under 12: $0.50", 55, 40, font, brightgreen)
    drawfillbox (25, 175, 500, 100, brightred)
    Font.Draw ("Students Under 18: $2.50", 55, 125, font, brightgreen)
    drawfillbox (25, 260, 500, 185, brightred)
    Font.Draw ("Adults 18-65: $5.00", 90, 208, font, brightgreen)
    drawfillbox (25, 345, 500, 270, brightred)
    Font.Draw ("Seniors over 65: $0.75", 75, 295, font, brightgreen)
    if x > 24 and x < 501 and y > 14 and y < 89 then
        drawfillbox (25, 90, 500, 15, brightblue)
        Font.Draw ("Children Under 12: $0.50", 55, 40, font, brightred)
        if b = 1 then
            bill := bill + 0.50
        end if
    end if
    if x > 24 and x < 501 and y > 99 and y < 176 then
        drawfillbox (25, 175, 500, 100, brightblue)
        Font.Draw ("Students Under 18: $2.50", 55, 125, font, brightred)
        if b = 1 then
            bill := bill + 2.50
        end if
    end if
end loop


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?
 
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Tue May 09, 2006 9:35 pm   Post subject: (No 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)

code:

has_clicked = false
loop
  If the mouse button is down and the mouse is over the button
    if has_clicked is false
      has_clicked = true
      Do button action
    end if
  end if
  if mouse button is up and has_clicked is true
    has_clicked = false
  end if
end loop


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?
 
Reality Check




PostPosted: Tue May 09, 2006 9:40 pm   Post subject: (No subject)

Wow man thanks a lot. I'd give you bits if I knew how...
 
Martin




PostPosted: Tue May 09, 2006 9:41 pm   Post subject: (No 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:

code:
has_clicked = false
loop
  If the mouse button is down and the mouse is over the button
    if has_clicked is false
      Do button action
    end if
  end if
  if the mouse button is down
    has_clicked = true
  elsif mouse button is up and has_clicked is true
    has_clicked = false
  end if
end loop
 
Reality Check




PostPosted: Tue May 09, 2006 9:48 pm   Post subject: (No subject)

^^Yea I was meaning to think of a way to do that too...you learn something new everyday!
 
Martin




PostPosted: Tue May 09, 2006 10:01 pm   Post subject: (No subject)

Means it was a well spent day. I'm glad to be a help. Smile
 
Zacebdal




PostPosted: 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.
code:
var button, buttoncheck, x, y, shots : int
shots := 0
loop
    mousewhere (x, y, button)
    if button = 1 and buttoncheck = 1 then
        shots := shots + 1
    end if
    if button = 0 then
        buttoncheck := 1
    elsif button = 1 then
        buttoncheck := 0
    end if
    put shots
    delay (5)
    cls
end loop

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. Smile
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: