Posted: 5th Dec 2021 4:32
Here is code I am using for input:
+ Code Snippet
function getInput()
	// store the joystick's x position
	joyX as float
	
	// get the joystick's x position
	joyX = GetVirtualJoystickX(OBJECT_ID_JOYSTICK)
	
	// if the joystick was pushed left
	if(joyX < 0)

		// if the cursor won't go out of bounds
		if cursor.x < -1

			// move the cursor left
			cursor.x = cursor.x - 1
		endif
	endif

	// if the joystick was pushed right
	if(joyX > 0)

		// if the cursor won't go out of bounds
		if cursor.x < BOARD_SIZE_WIDTH

			// move the cursor right
			cursor.x = cursor.x + 1
		endif
	endif

The issue is cursor.x is incremented very very quickly. Is there some way to make it so it only increments each time the stick is pushed instead of held?
Posted: 5th Dec 2021 5:18
when you push any joystick it equals something around 8 or -8

What I do is say

if(joyX = 3)
then this happens
endif
Posted: 5th Dec 2021 5:34
when you push any joystick it equals something around 8 or -8
huh?

@OWI, that's going to be tricky with a virtual joystick because they emulate digital joysticks with a range between -1.0 and 1.0 but the idea would otherwise be along the lines of :
+ Code Snippet
SetErrorMode(2)

// set window properties
SetWindowTitle( "Joy1" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) 
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )

GLOBAL CursorX, LastJoyX
GLOBAL BOARD_SIZE_WIDTH = 20
AddVirtualJoystick(1,200,200,50)
do
	Input()
	
    If GetRawKeyState(27) then Exit    

    Print( CursorX )
    print(GetVirtualJoystickX(1))
    Sync()
loop

Function Input()
	JoyX = GetVirtualJoystickX(1)
	if LastJoyX = 0
		If JoyX < 0 and CursorX > 0 then DEC CursorX
		If JoyX > 0 and CursorX < BOARD_SIZE_WIDTH then INC CursorX
	Endif
	LastJoyX = JoyX
EndFunction

note the value of GetVirtualJoystickX() when you release it where it gradually returns to center/0. so, just because you "released" it doesn't mean it's 0/centered/ready for another tap.

but, to detect a move in either direction, you'll need to compare it to "unmoved", somehow.

that being said, if you're intent on using a vjoystick to move the cursor, you may simply want to put a timer on it to "slow it down":
+ Code Snippet
SetErrorMode(2)

// set window properties
SetWindowTitle( "Joy1" )
SetWindowSize( 1280,720, 0 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1280,720)
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) 
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )

GLOBAL CursorX
GLOBAL BOARD_SIZE_WIDTH = 20
AddVirtualJoystick(1,200,200,50)
do
	If LastMove# + 0.5 <= Timer()
		Input()
		LastMove# = Timer()
	Endif
	
    If GetRawKeyState(27) then Exit    

    Print( CursorX )
    print(GetVirtualJoystickX(1))
    Sync()
loop

Function Input()
	If GetVirtualJoystickX(1) < 0 and CursorX > 0 then DEC CursorX
	If GetVirtualJoystickX(1) > 0 and CursorX < BOARD_SIZE_WIDTH then INC CursorX
EndFunction
Posted: 5th Dec 2021 5:54
that's going to be tricky with a virtual joystick because they emulate digital joysticks with a range between -1.0 and 1.0 but the idea would otherwise be along the lines of :


Whoops lol, That is correct.

I am always thinking a real joystick, sorry.
Posted: 5th Dec 2021 7:26
Timers are a Programmers Best Friend...
In fact I'd say that the MOST important Procedure to Memorise is:

+ Code Snippet
OldTime# = NewTime#
NewTime# = Timer( )
Duration# = NewTime# - OldTime#
Inc ElapsedTime#, Duration#


Using Timers to limit how often you "Run" Components will ensure that updates are Consistent., thus you can adapt update values based upon said Timer Period.
Esp. if we ALWAYS Multiply by the Duration between the Timer Calls.
Posted: 5th Dec 2021 11:04
Timers are a Programmers Best Friend...


Agreed.