Joystick Permanent Fire

Advanced game related topics
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Joystick Permanent Fire

Post by jamirokwai »

Hi there,

I searched around the forum, but didn't find an answer to my "problem".
I am using Purebasics Joystick-functions like in the example from the docs:

Code: Select all

NbJoysticks = InitJoystick()

If NbJoysticks = 0
  MessageRequester("Error", "No joysticks are availables.", 0)
  End
EndIf

If OpenWindow(0, 100, 100, 300, 260, "PureBasic - Joystick Demo")

  x = WindowWidth(0)/2
  y = WindowHeight(0)/2-20

  Repeat
  
    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow : Quit = 1 : EndIf
    Until Event = 0

    Delay(20) ; a little delay (20 milli seconds -> 50 fps)
  
    If ExamineJoystick(0)
      x+JoystickAxisX(0)
      y+JoystickAxisY(0)
     
      If JoystickButton(0, 1)
        Debug "button 1"
      EndIf
      
      If JoystickButton(0, 2)
        Debug "button 2"
      EndIf
    
      If StartDrawing(WindowOutput(0)) ; Set the drawing output to our window
        FrontColor(RGB(255,0,0))      ; Use the RED colour
        Box(x, y, 10, 10)         ; Draw a little box
        StopDrawing()             ;
      EndIf
    EndIf

  Until Quit = 1

EndIf

End
Works great. BUT: The above routine will output "button 1" endlessly, until I lift my finger from button 1.

How do you people handle this? I would need button_down and button_up instead of button_pushed, like the Canvas-events for mouse clicks.
I am thinking about a timeout handler like 500 msec, but that won't help with permanent fire. It may also break a game.

Any ideas welcome and very much appreciated :-)
Regards,
JamiroKwai
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Joystick Permanent Fire

Post by Caronte3D »

Someting like that?

Code: Select all

Static jb
If JoystickButton(0, 1) And Not jb
  jb = 1
  Debug "button 1"
Else
  jb = 0
EndIf
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Joystick Permanent Fire

Post by jamirokwai »

Caronte3D wrote: Wed Apr 05, 2023 7:41 pm Someting like that?

Code: Select all

Static jb
If JoystickButton(0, 1) And Not jb
  jb = 1
  Debug "button 1"
Else
  jb = 0
EndIf
I was just thinking about that idea. Will test it out. Thanks!
Regards,
JamiroKwai
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Joystick Permanent Fire

Post by jamirokwai »

Hi there,

long delay, but. That does not work. It switches between 1 and 0 permanently. At least with my gamepad. But thanks for the idea!

I think, I will go with a timed event, as I have an open window in my application.

Edit: it works! Just have to do it right, and without migraine… thanks, Caronte3D!
Regards,
JamiroKwai
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Joystick Permanent Fire

Post by mk-soft »

Code: Select all

Static jb
If JoystickButton(0, 1)
  If Not jb
    jb = 1
    Debug "button 1"
  EndIf
Else
  jb = 0
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Joystick Permanent Fire

Post by Caronte3D »

Sorry, I was wrote it from memory :?

Thi's tested with your code:

Code: Select all

NbJoysticks = InitJoystick()

If NbJoysticks = 0
  MessageRequester("Error", "No joysticks are availables.", 0)
  End
EndIf

If OpenWindow(0, 100, 100, 300, 260, "PureBasic - Joystick Demo")
  
  x = WindowWidth(0) / 2
  y = WindowHeight(0) / 2 - 20
  
  Define jb = 0, jb2 = 0
  Repeat
    
    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow : Quit = 1 : EndIf
    Until Event = 0
    
    Delay(20) ; a little delay (20 milli seconds -> 50 fps)
    
    If ExamineJoystick(0)
      x + JoystickAxisX(0)
      y + JoystickAxisY(0)
      
      If JoystickButton(0, 1) And Not jb
        jb = 1
        Debug "button 1"
      ElseIf Not JoystickButton(0, 1)
        jb = 0
      EndIf
      
      If JoystickButton(0, 2) And Not jb2
        jb2 = 1
        Debug "button 2"
      ElseIf Not JoystickButton(0, 2)
        jb2 = 0
      EndIf
      
      If StartDrawing(WindowOutput(0)) ; Set the drawing output to our window
        FrontColor(RGB(255, 0, 0))      ; Use the RED colour
        Box(x, y, 10, 10)         ; Draw a little box
        StopDrawing()             ;
      EndIf
    EndIf
    
  Until Quit = 1
  
EndIf

End
Anyway, the code from Mk-Soft is better :wink:
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Joystick Permanent Fire

Post by jamirokwai »

mk-soft wrote: Thu Apr 13, 2023 9:08 pm

Code: Select all

Static jb
If JoystickButton(0, 1)
  If Not jb
    jb = 1
    Debug "button 1"
  EndIf
Else
  jb = 0
EndIf
Nice, thanks.

I am using this one now:

Code: Select all

If JoystickButton(0, GamePad::#gamepad_button_a)
      Debug "fire"
    Else
      Debug "no fire"
    EndIf
because my painting app will draw as long as you press a button - when in pencil-mode.

MK-Soft, Caronte, your examples will help me implementing e.g. drawing circles, as I need a center and the radius, which are set when pushing the button only once. Thanks a lot!
Regards,
JamiroKwai
Post Reply