Author Topic: Two beeps?  (Read 4304 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Two beeps?
« on: February 23, 2020, 11:52:46 am »
Why does this code beep twice when you click once:

Code: QB64: [Select]
  1.     mb = 0
  2.  
  3.         x = _MOUSEX
  4.         y = _MOUSEY
  5.         IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  6.             IF _MOUSEBUTTON(1) AND (mb = 0) THEN
  7.                 BEEP
  8.                 mb = 1
  9.                 _DELAY .1
  10.             ELSE
  11.                 mb = 0
  12.             END IF
  13.         END IF
  14.     LOOP
  15.  
  16.     _LIMIT 60
  17.  

whereas this code beeps once when you click once:

Code: QB64: [Select]
  1.     mb = 0
  2.  
  3.         x = _MOUSEX
  4.         y = _MOUSEY
  5.         IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  6.             IF _MOUSEBUTTON(1) THEN
  7.                 IF (mb = 0) THEN
  8.                     BEEP
  9.                     mb = 1
  10.                     _DELAY .1
  11.                 END IF
  12.             ELSE
  13.                 mb = 0
  14.             END IF
  15.         END IF
  16.     LOOP
  17.  
  18.     _LIMIT 60
  19.  

All I've done is fragment the IF statement into two lines, getting rid of an AND operator, and god knows what else under the hood. Can someone straighten me out?
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Two beeps?
« Reply #1 on: February 23, 2020, 12:03:11 pm »
Because (0 AND 0) might just be true!

No it's the ELSE clause
Code: QB64: [Select]
  1.     mb = 0
  2.  
  3.         x = _MOUSEX
  4.         y = _MOUSEY
  5.         IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  6.             IF _MOUSEBUTTON(1) AND (mb = 0) THEN
  7.                 BEEP
  8.                 mb = 1
  9.                 _DELAY .1
  10.             ELSE '<<<<<<<<<<<<<<<<<<<<<   take out
  11.                 'mb = 0
  12.             END IF
  13.         END IF
  14.     LOOP
  15.  
  16.     _LIMIT 60
  17.  

You don't need any ELSE.
« Last Edit: February 23, 2020, 12:10:06 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Two beeps?
« Reply #2 on: February 23, 2020, 12:08:42 pm »
Oh maybe, yeah that might be it. Yeah that's subtle. Nice catch.

Funny enough the part that you said "take out" just got nixed from my main code. I've been going too fast. Thanks for being a second set of eyes.
« Last Edit: February 23, 2020, 12:10:01 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Two beeps?
« Reply #3 on: February 24, 2020, 02:42:37 am »
The beeping problem with beeps is they take up time. Make a beep, and the time it takes ruins any possibility of recording multiple clicks, like a classic double click, for example. We can get around that with a beeping timer delay, as follows...

Code: QB64: [Select]
  1.     IF mb THEN IF _MOUSEBUTTON(1) = 0 THEN mb = 1
  2.     x = _MOUSEX
  3.     y = _MOUSEY
  4.     IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  5.         IF _MOUSEBUTTON(1) THEN
  6.             z1 = TIMER
  7.             IF mb >= 0 THEN click = click + 1
  8.             mb = -1
  9.         END IF
  10.         IF z1 THEN
  11.             IF ABS(z1 - TIMER) >= .2 THEN
  12.                 PRINT "Number of clicks = "; click
  13.                 FOR i = 1 TO click: BEEP: NEXT: click = 0
  14.                 z1 = 0
  15.             END IF
  16.         END IF
  17.     END IF
  18.     _LIMIT 60
  19.  


Note: If you're fast enough, this will record triple clicks and maybe even a speed demon like BPlus can squeak out a bit more.

You're beeping welcome,

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Two beeps?
« Reply #4 on: February 24, 2020, 03:09:54 am »
On my computer (Win10 Home, version 1903, 64-bit, 3.80 GHz) running QB64 x32 version 1.4, both code examples beep only once for each mouse click!
The double beep does not happen.
Here is the code that I used for testing (basically the same as what STxAxTIC provided).

Code: QB64: [Select]
  1. ' uncomment one of the following
  2. $LET TWOBEEPS = 1
  3. '$LET ONEBEEP = 1
  4.  
  5.  
  6. $IF TWOBEEPS THEN
  7.     PRINT "TWO BEEPS"
  8.     DO
  9.         mb = 0
  10.  
  11.         DO WHILE _MOUSEINPUT
  12.             x = _MOUSEX
  13.             y = _MOUSEY
  14.             IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  15.                 IF _MOUSEBUTTON(1) AND (mb = 0) THEN
  16.                     BEEP
  17.                     mb = 1
  18.                     _DELAY .1
  19.                 ELSE
  20.                     mb = 0
  21.                 END IF
  22.             END IF
  23.         LOOP
  24.  
  25.         _LIMIT 60
  26.     LOOP
  27.  
  28. $IF ONEBEEP THEN
  29.     PRINT "ONE BEEP"
  30.     DO
  31.         mb = 0
  32.  
  33.         DO WHILE _MOUSEINPUT
  34.             x = _MOUSEX
  35.             y = _MOUSEY
  36.             IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  37.                 IF _MOUSEBUTTON(1) THEN
  38.                     IF (mb = 0) THEN
  39.                         BEEP
  40.                         mb = 1
  41.                         _DELAY .1
  42.                     END IF
  43.                 ELSE
  44.                     mb = 0
  45.                 END IF
  46.             END IF
  47.         LOOP
  48.  
  49.         _LIMIT 60
  50.     LOOP
  51.  
  52.  
  53.  
« Last Edit: February 24, 2020, 03:19:49 am by EricE »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Two beeps?
« Reply #5 on: February 24, 2020, 10:19:49 am »
you might want to clear the mouse buffer too. Unless your the flash with those fingers!
Place this toward the bottom of your loop. It may not be the only issue but it wont hurt either.
DO:LOOP WHILE _MOUSEINPUT

that way there are no extra button clicks in there.

As the IF states mousebutton AND mb=0 the else doesn't really matter unless there is an extra button press to AND with it.  Nowif it was an OR then multiple beeps can ensue(though it would be more than 2)

what I am trying to say is  FALSE and TRUE don't pass go.  so if no mouse click and MB=0 you can't beep.
(and mousebutton returns 0 or -1)

Because (0 AND 0) might just be true!
You were joking right Bplus!??

STxAxTIC, I take it you mean you click on that area ONCE and get TWO beeps? or do you mean you click on the area multiple times and still get a beep??
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Two beeps?
« Reply #6 on: February 24, 2020, 11:56:46 am »
The beeping problem with beeps is they take up time. Make a beep, and the time it takes ruins any possibility of recording multiple clicks, like a classic double click, for example. We can get around that with a beeping timer delay, as follows...

Code: QB64: [Select]
  1.     IF mb THEN IF _MOUSEBUTTON(1) = 0 THEN mb = 1
  2.     x = _MOUSEX
  3.     y = _MOUSEY
  4.     IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  5.         IF _MOUSEBUTTON(1) THEN
  6.             z1 = TIMER
  7.             IF mb >= 0 THEN click = click + 1
  8.             mb = -1
  9.         END IF
  10.         IF z1 THEN
  11.             IF ABS(z1 - TIMER) >= .2 THEN
  12.                 PRINT "Number of clicks = "; click
  13.                 FOR i = 1 TO click: BEEP: NEXT: click = 0
  14.                 z1 = 0
  15.             END IF
  16.         END IF
  17.     END IF
  18.     _LIMIT 60
  19.  


Note: If you're fast enough, this will record triple clicks and maybe even a speed demon like BPlus can squeak out a bit more.

You're beeping welcome,

Pete

Cool Game!
Fibonacci Game.PNG
* Fibonacci Game.PNG (Filesize: 4.19 KB, Dimensions: 254x188, Views: 323)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Two beeps?
« Reply #7 on: February 24, 2020, 12:02:03 pm »
On my computer (Win10 Home, version 1903, 64-bit, 3.80 GHz) running QB64 x32 version 1.4, both code examples beep only once for each mouse click!
The double beep does not happen.
Here is the code that I used for testing (basically the same as what STxAxTIC provided).

Code: QB64: [Select]
  1. ' uncomment one of the following
  2. $LET TWOBEEPS = 1
  3. '$LET ONEBEEP = 1
  4.  
  5.  
  6. $IF TWOBEEPS THEN
  7.     PRINT "TWO BEEPS"
  8.     DO
  9.         mb = 0
  10.  
  11.         DO WHILE _MOUSEINPUT
  12.             x = _MOUSEX
  13.             y = _MOUSEY
  14.             IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  15.                 IF _MOUSEBUTTON(1) AND (mb = 0) THEN
  16.                     BEEP
  17.                     mb = 1
  18.                     _DELAY .1
  19.                 ELSE
  20.                     mb = 0
  21.                 END IF
  22.             END IF
  23.         LOOP
  24.  
  25.         _LIMIT 60
  26.     LOOP
  27.  
  28. $IF ONEBEEP THEN
  29.     PRINT "ONE BEEP"
  30.     DO
  31.         mb = 0
  32.  
  33.         DO WHILE _MOUSEINPUT
  34.             x = _MOUSEX
  35.             y = _MOUSEY
  36.             IF (x > 0) AND (x < _WIDTH) AND (y > 0) AND (y < _HEIGHT) THEN
  37.                 IF _MOUSEBUTTON(1) THEN
  38.                     IF (mb = 0) THEN
  39.                         BEEP
  40.                         mb = 1
  41.                         _DELAY .1
  42.                     END IF
  43.                 ELSE
  44.                     mb = 0
  45.                 END IF
  46.             END IF
  47.         LOOP
  48.  
  49.         _LIMIT 60
  50.     LOOP
  51.  
  52.  
  53.  


" ... (basically the same as what STxAxTIC provided)."

Except you are using metacommands???  and
Code: QB64: [Select]
  1. $LET TWOBEEPS = 1
fuzzy math ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Two beeps?
« Reply #8 on: February 24, 2020, 12:18:20 pm »
Quote
Quote from: bplus on Yesterday at 12:03:11 PM
Because (0 AND 0) might just be true!
You were joking right Bplus!??

I was looking at
Code: QB64: [Select]
  1. IF _MOUSEBUTTON(1) AND (mb = 0) THEN
  2.  

and seeing when mb = 0 then (mb = 0) evaluates to true so if (_MOUSEBUTTON(1) = 0) also you have a true evaluation but we don't have IF (_MOUSEBUTTON(1) = 0) AND (mb = 0) THEN but it was sorta like having two things = 0 so getting a true evaluation from AND but I started typing before my brain caught up with my fingers, thought it kinda funny so kept it. So yeah, joking :)
« Last Edit: February 24, 2020, 12:21:01 pm by bplus »