August 04, 2020, 11:37:56 AM

Author Topic: Multiple keys pressed at the same time  (Read 228 times)

Offline paulvern

  • Jr. Member
  • **
  • Posts: 2
Multiple keys pressed at the same time
« on: June 25, 2020, 01:54:29 PM »
I'm back to Blitz after almost 30 years! (yes, I used it back in Amiga times... a age ago).
Many things are not the same but many others didn't change a lot during this time.
I don't understand what's wrong with this little piece of code:
Code: [Select]
Graphics 800,600
SetClsColor 255,255,255
Global My_Image:TImage=LoadAnimImage("h:/Cockroach.png",32,32,0,4)
Local Rota:Int=90
Local frame:Int=0
Local x#
Local y#
While Not KeyDown(Key_Escape)
If KeyDown(Key_Right)
If KeyDown(Key_Left)
If KeyDown(Key_up)
SetRotation( Rota)
DrawImage( My_image,200+x,200+y,frame )
If frame=3 Then frame=0

I'm loading the image of a cockroach and trying to get key inputs. Everything works fine as long as I don't press two keys at the same time. If I try to press two keys I receive the following error message: attempt to index array element beyond array length.
Can't multiple key presses be handled this way?
I hope I'm missing something.

Offline Juiceter

  • Jr. Member
  • **
  • Posts: 52
Re: Multiple keys pressed at the same time
« Reply #1 on: July 06, 2020, 12:28:48 AM »
maybe try a flushkeys after the initial key has been chosen? No two key strokes are ever done at (exactly) the same time! And yes, I used to code in blitz basic on the amiga too. Heady days eh?!  ;)

Offline GfK

  • Jr. Member
  • **
  • Posts: 78
Re: Multiple keys pressed at the same time
« Reply #2 on: July 06, 2020, 10:25:21 AM »
I'd imagine from that code, that your "frame" is going beyond the number of frames you've actually got.  By the look of it, that's either greater than 3, or less than zero.

So, clamp 'frame' between 0 and 3 BEFORE using DrawImage.
Intel I9-9900K 3.6-5.0GHz | GeForce RTX2070 8GB | 32GB RAM | 500GB NVMe M.2 SSD | 1TB HDD | Windows 10 x64.

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 492
Re: Multiple keys pressed at the same time
« Reply #3 on: July 06, 2020, 12:28:56 PM »
You are updating frame with each keypress.  So if you press left and up at the same time, frame gets updated twice causing it to exceed 3 and causing the index error when you try DrawImage().  You should instead set a flag, update frame only when the flag is set, and make sure it is within 0-3 before the draw.
Code: BlitzMax
  1. Strict
  2. Graphics 800,600
  3. SetClsColor 255,255,255
  4. AutoMidHandle(True)
  5. Global My_Image:TImage=LoadAnimImage("h:/Cockroach.png",32,32,0,4)
  6. Local Rota:Int=90
  7. Local frame:Int=0
  8. Local x#
  9. Local y#
  10. Local Animate:Int = False
  11. While Not KeyDown(Key_Escape)
  12.         Cls
  13.         If KeyDown(Key_Right)
  14.                 Rota:+1
  15.                 Animate = True
  16.         EndIf
  17.         If KeyDown(Key_Left)
  18.                 Rota:-1
  19.                 Animate = True
  20.         EndIf
  21.         If KeyDown(Key_up)
  22.                 Animate = True
  23.                 x=x+2*Cos(Rota-90)
  24.                 y=y+2*Sin(Rota-90)
  25.         EndIf
  26.         SetRotation( Rota)
  27.         If Animate
  28.                 Animate = False
  29.                 frame :+ 1
  30.                 If frame >= 4 Then frame = 0
  31.         EndIf
  32.         DrawImage( My_image,200+x,200+y,frame )
  33.         Flip
  34. Wend
8 rabbits equals 1 rabbyte.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3074
Re: Multiple keys pressed at the same time
« Reply #4 on: July 06, 2020, 10:00:28 PM »
The issue was resolved one day after posting here. He posted in the "bmx ng"-discord channel and I (and others) gave already quick hints on what is faulty in his code.

Maybe he will post here once he runs into the next problem (he did not post since short after that issue so maybe he is just busy doing other stuff).


Offline paulvern

  • Jr. Member
  • **
  • Posts: 2
Re: Multiple keys pressed at the same time
« Reply #5 on: July 16, 2020, 08:26:46 AM »
Sorry for the long delay in my reply here. I've been away for vacations. Thank you all for the tips. And yes, the problem is fixed now.


SimplePortal 2.3.6 © 2008-2014, SimplePortal