I'm currently setting up my game to support both keyboard and joypad control and so far it’s working flawlessly, save for one thing...
If for example I need to call Keyhit(KEY_UP), I can't see any obvious equivalent command for the joypad, only JoyY()=-1 which is effectively the same as KeyDown(KEY_UP).
Does anyone have any idea of an easy way to make a JoyAxisHit() command that will only register the joystick movement once?
Thanks!
Isn't it Joyhit (pub.freejoy 1.08)?
As far as I am aware, Joyhit just works for jostick buttons, not the axis :(
Const joyDeadZone# = 0.5
Rem
'You could do something like this instead
Global joyDeadZone#
For c=1 To 1000
If Abs(JoyY()) > joyDeadZone Then joyDeadZone = Abs(JoyX())
If Abs(JoyY()) > joyDeadZone Then joyDeadZone = Abs(JoyY())
Next
joyDeadZone :* 1.1 ' Just to be on the safe side
End Rem
Graphics 800, 600, 0
Local ox = 400, oy = 300
Repeat
ox :+ (JoyXHit() * 40)
oy :+ (JoyYHit() * 40)
Cls
SetColor 200, 200, 0
DrawOval ox - 20, oy - 20, 40, 40
Flip
Until KeyHit(KEY_ESCAPE)
End
Function JoyXHit(port=0)
Global centered = True
Local jx# = JoyX(port)
If centered
If jx# > joyDeadZone#
centered = False
Return 1
ElseIf jx# < -joyDeadZone#
centered = False
Return -1
EndIf
ElseIf (jx# >= -joyDeadZone#) And (jx# <= joyDeadZone#)
centered = True
EndIf
End Function
Function JoyYHit(port=0)
Global centered = True
Local jy# = JoyY(port)
If centered
If jy# > joyDeadZone#
centered = False
Return 1
ElseIf jy# < -joyDeadZone#
centered = False
Return -1
EndIf
ElseIf (jy# >= -joyDeadZone#) And (jy# <= joyDeadZone#)
centered = True
EndIf
End Function
Awesome! Thanks very much, that's got it sorted :)
Actually, this doesn't work correctly...
Const joyDeadZone# = 0.5
Graphics 800, 600, 0
Global jdir$="NOTHING"
Repeat
DrawText jdir+" was the last key to be pressed",10,300
If joyxhit()=1 Then jdir="RIGHT"
If joyxhit()=-1 Then jdir="LEFT"
If joyyhit()=1 Then jdir="DOWN"
If joyyhit()=-1 Then jdir="UP"
Flip
Cls
Until KeyHit(KEY_ESCAPE)
Function JoyXHit(port=0)
Global centered = True
Local jx# = JoyX(port)
If centered
If jx# > joyDeadZone#
centered = False
Return 1
ElseIf jx# < -joyDeadZone#
centered = False
Return -1
EndIf
ElseIf (jx# > -joyDeadZone#) And (jx# < joyDeadZone#)
centered = True
EndIf
End Function
Function JoyYHit(port=0)
Global centered = True
Local jy# = JoyY(port)
If centered
If jy# > joyDeadZone#
centered = False
Return 1
ElseIf jy# < -joyDeadZone#
centered = False
Return -1
EndIf
ElseIf (jy# > -joyDeadZone#) And (jy# < joyDeadZone#)
centered = True
EndIf
End Function
the if Statements only work for the positive values (whichever comes first in the code)....curious. Any ideas on how I can fix that?
Maybe just call joyxhit() once, store the returned value and use that in the if statement.
xret = joyxhit()
if xret = 1...
if xret = -1...