
-----------------------------------
Reality Check
Tue May 09, 2006 9:18 pm

Making a button press occur only once.
-----------------------------------
Alright I'm doing this little thing for school.  Heres the code before I go further.

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?

-----------------------------------
Martin
Tue May 09, 2006 9:35 pm


-----------------------------------
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)


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
Tue May 09, 2006 9:40 pm


-----------------------------------
Wow man thanks a lot.  I'd give you bits if I knew how...

-----------------------------------
Martin
Tue May 09, 2006 9:41 pm


-----------------------------------
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:

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
Tue May 09, 2006 9:48 pm


-----------------------------------
^^Yea I was meaning to think of a way to do that too...you learn something new everyday!

-----------------------------------
Martin
Tue May 09, 2006 10:01 pm


-----------------------------------
Means it was a well spent day. I'm glad to be a help. :)

-----------------------------------
Zacebdal
Tue Oct 17, 2006 8:15 pm

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.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. :)
