Blitz3D+ Command Reference

JoyYDir ( [port] )

Parameters

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

Description

Returns which way the joystick is pushed on the y-axis, as -1, 0 or 1.

It is JoyY with the dead zone already applied:
  -1: pushed forward (up)
   0: centred
   1: pulled back (down)

The stick must travel more than a third of the way from centre before the value changes, so a resting stick reliably reads 0. Together with JoyXDir it gives you the eight-way input a digital pad or an old-school arcade game expects, in two lines and with no threshold code of your own.

Note the sign: -1 is up. That matches screen coordinates, so adding the value to a y position moves things the way the player pushed. If you are moving a character in a 3D world you will usually want to negate it.

Heads up: the current DirectX 12 runtime does not enumerate joysticks - it reports zero attached devices - so JoyYDir returns 0 on every port, as does an out-of-range port number. Guard joystick controls with JoyType and keep a keyboard alternative.

See also: JoyXDir, JoyZDir, JoyY, JoyType, JoyDown.

Example

; JoyYDir Example
; ---------------

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

y=240

While Not KeyDown(1)

    Cls

    ; JoyYDir returns a clean digital direction for the y-axis:
    ; -1 (up), 0 (centred) or 1 (down) - perfect for d-pads.
    ; JoyXDir and JoyZDir do the same for the other axes.
    d=JoyYDir()

    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, the d-pad slides the player block."
    Else
        Text 0,0,"Press the d-pad / stick up and down   Esc: exit"
    End If

    ; Slide the player block at a fixed speed in the digital direction
    y=y+d*4
    If y<120 Then y=120
    If y>460 Then y=460
    Rect 305,y-15,30,30,True

    Text 0,20,"JoyYDir() = "+d

    Flip

Wend

End

Index