Blitz3D+ Command Reference

JoyHit ( button[,port] )

Parameters

button - number of the joystick button to test, counting from 1

port (optional) - which joystick to read; 0 is the first stick (default)

Description

Returns how many times a joystick button has been pressed since you last asked.

This is the pad version of KeyHit, and the one to use for anything that should happen once per press: jumping, firing a single shot, confirming a menu choice, cycling a weapon. Holding the button down still counts as one hit - JoyDown is the command for "still held".

Reading the count resets it to zero, so only one place in your code should test a given button each frame. If two systems need it, read it once into a variable and share that. FlushJoy clears every counter at once, which is worth doing when a new screen appears.

Heads up: the current DirectX 12 runtime does not enumerate joysticks - it reports zero attached devices - so JoyHit returns 0 on every port and button, and an out-of-range port returns 0 rather than raising an error. Check JoyType at startup and keep a keyboard alternative for everything important.

See also: JoyDown, GetJoy, JoyType, FlushJoy, KeyHit, MouseHit.

Example

; JoyHit Example
; --------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

count=0
flash=0

While Not KeyDown(1)

    Cls

    If JoyType()=0 Then
        Text 0,0,"Esc: exit"
        Text 0,60,"No joystick detected - plug in a gamepad to try this example."
        Text 0,80,"With one attached, tapping button 1 bumps the counter below."
    Else
        Text 0,0,"Tap joystick button 1 (holding does not autofire)   Esc: exit"
    End If

    ; JoyHit returns how many times the button was PRESSED since the
    ; last call - holding it down counts only once.
    ; (Compare JoyDown, which stays 1 while the button is held.)
    hits=JoyHit(1)
    count=count+hits
    If hits>0 Then flash=10

    ; A muzzle flash marks each fresh press
    If flash>0 Then
        flash=flash-1
        Color 255,220,0
        Oval 295,215,50,50,True
        Color 255,255,255
    End If

    Text 0,20,"JoyHit(1) presses counted so far: "+count

    Flip

Wend

End

Index