Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DO-LOOP crashing
#1
I made a DO loop in my program to record the key presses. By the way, it is made in FreeBasic. Here is some of it:

Code:
DO UNTIL INKEY$ <> ""

SLEEP 60 'uses milliseconds in FreeBasic

press$ = INKEY$
scancode% = ASC(RIGHT$(press$, 1))

  SELECT CASE scancode%

    CASE IS = 72
      IF row = 1 THEN GOTO 2
        
      row = row - 1
      LOCATE row, col  
      
      IF drawmode = 1 THEN
        FOR count = 1 to 20
          IF row = count THEN MID$(map$(count), col) = dblock$
        NEXT count
      END IF

    CASE IS = 80
      IF row = 20 THEN GOTO 2
        
      row = row + 1
      LOCATE row, col
        
      IF drawmode = 1 THEN
        FOR count = 1 to 20
          IF row = count THEN MID$(map$(count), col) = dblock$
        NEXT count
      END IF

    ..... (more keystrokes)

    LOCATE row, col
    scancode% = 0

LOOP

For some reason, it suddenly crashes when I do not press anything for a couple seconds then try to press something. I have no idea what it could be, so help would be appreciated.
Reply
#2
Try:

Do until Press$<>""
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
Exactly. The problem is in this line:

Code:
scancode% = ASC(RIGHT$(press$, 1))

If you press nothing, press$ = "", and you can't extract 1 character from the right from an empty string, so you get a crash.

You can avoid the error wether using Rel's suggestion or just:

Code:
IF press$<>"" THEN scancode% = ASC(RIGHT$(press$, 1)) ELSE scancode% = 0
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#4
It's usually faster to compare the length of a string to zero than to use a string comparison with an empty string... i.e. 'if len(xyz$) = 0 ...' rather than 'if xyz$ = ""'.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)