JoyXDir ( [port] )
Parameters
| port (optional) - which joystick to read; 0 is the first stick (default) |
Description
|
Returns which way the joystick is pushed on the x-axis, as -1, 0 or 1. It is JoyX with the dead zone already done for you: -1: pushed left 0: centred 1: pushed right The stick has to travel more than a third of the way from centre before the value flips, which keeps a resting stick reading 0 instead of jittering between directions. That makes it perfect for digital pads and for any game where movement is a simple step - a platformer, a puzzle grid, a menu cursor - where you would otherwise be writing the same threshold test yourself. For driving, flying or anything that wants proportional steering, read JoyX instead and multiply the float by your turn rate. Heads up: the current DirectX 12 runtime does not enumerate joysticks - it reports zero attached devices - so JoyXDir returns 0 on every port, as does an out-of-range port number. Check JoyType before building controls on it and keep a keyboard route to everything. See also: JoyYDir, JoyZDir, JoyX, JoyType, JoyDown. |
Example
; JoyXDir Example ; --------------- Graphics 640,480,0,2 SetBuffer BackBuffer() x=320 While Not KeyDown(1) Cls ; JoyXDir returns a clean digital direction for the x-axis: ; -1 (left), 0 (centred) or 1 (right) - perfect for d-pads. ; JoyYDir and JoyZDir do the same for the other axes. d=JoyXDir() 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 left and right Esc: exit" End If ; Slide the player block at a fixed speed in the digital direction x=x+d*4 If x<20 Then x=20 If x>620 Then x=620 Rect x-15,300,30,30,True Text 0,20,"JoyXDir() = "+d Flip Wend End
Index