Page 1 of 1

Keayboard action

Posted: Mon May 15, 2017 4:10 pm
by PowerSoft
I wont to change the variable FacingAzimuth with the left- or right key.
The code is working but when I tough the key for a short moment PB is so fast the value is change to much.
How can I change the the code in the way that the value is increment or decrement with one independent of how long I
push the button.

Any help will be great

Code: Select all

      ExamineKeyboard()
      If KeyboardReleased(#PB_Key_Escape)
        Exit = #True
      EndIf  
      
      If KeyboardPushed(#PB_Key_Left)
        ;Debug "#PB_Key_Left"
        FacingAzimuth=FacingAzimuth-1
        If FacingAzimuth < 0
          FacingAzimuth = 0
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Right)
        ;Debug "#PB_Key_Right"
        FacingAzimuth=FacingAzimuth+1
        If FacingAzimuth > 360
          FacingAzimuth = 360
        EndIf
      EndIf

Re: Keayboard action

Posted: Mon May 15, 2017 4:25 pm
by netmaestro
Try a Delay(<some value>) after If KeyboardPushed... and before FacingAzimuth + 1

A delay of 30 or so might do, but try values until you hit one you like.

Re: Keayboard action

Posted: Mon May 15, 2017 4:40 pm
by kenmo
Use something like:

Code: Select all

LeftPrev = LeftNow
LeftNow = KeyboardPushed(#PB_Key_Left)
If (LeftNow and (Not LeftPrev))
  ;Debug "#PB_Key_Left"
  FacingAzimuth=FacingAzimuth-1
  If FacingAzimuth < 0
    FacingAzimuth = 0
  EndIf
EndIf

Re: Keayboard action

Posted: Mon May 15, 2017 4:43 pm
by kenmo
If you want the angle to continue changing while key is held, try something like:

Code: Select all

If KeyboardPushed(#PB_Key_Left)
  If (LeftCount = 0)
    ;Debug "#PB_Key_Left"
    FacingAzimuth=FacingAzimuth-1
    If FacingAzimuth < 0
      FacingAzimuth = 0
    EndIf
  EndIf
  LeftCount = (LeftCount + 1) % 10
Else
  LeftCount = 0
EndIf
Or using floats:

Code: Select all

If KeyboardPushed(#PB_Key_Left)
  ;Debug "#PB_Key_Left"
  FacingAzimuth.f=FacingAzimuth-0.1
  If FacingAzimuth < 0
    FacingAzimuth = 0
  EndIf
EndIf