Here is a fully working example (copy, paste, run) illustrating the issue.
SetVirtualResolution (960,640)
Type tTouchEvent
ActiveFlag as Integer
StartX# as Float
StartY# as Float
EndX# as Float
EndY# as Float
SpriteTouched as Integer
JoystickStaked as Integer
EndType
global Dim TouchEvents[5] as tTouchEvent
Global mydistance# as Float
global joystickOuter,JoystickInner as Integer
joystickOuter=CreateSprite(0)
joystickInner=CreateSprite(0)
SetSpriteScale(joystickOuter,20,20)
SetSpriteColor(joystickOuter,255,0,0,255)
SetSpriteScale(joystickInner,8,8)
SetSpriteColor(joystickInner,255,255,0,255)
AddVirtualButton(1,800,40,80)
SetVirtualButtonText(1,"quit")
PositionMyVirtualJoyStick(500,300)
do
doMyVirtualJoystick()
print("distance="+str(mydistance#))
Sync()
if GetVirtualButtonPressed(1)=1 then end
loop
end
function doMyVirtualJoystick()
TouchEventID=GetRawFirstTouchEvent(1) // ID's start at 1
while TouchEventID>0
// got some events and maintain them active if they exist
TouchEvents[TouchEventID].ActiveFlag=1
TouchEventID=GetRawNextTouchEvent()
endwhile
// process the (actual) touch events
for x=1 to 5
// check touch was let go, so clear it
if (TouchEvents[x].ActiveFlag=1) and (GetRawTouchReleased(x)=1)
TouchEvents[x].ActiveFlag=0
TouchEvents[x].JoystickStaked=0
endif
// check if touch is active and process
if TouchEvents[x].ActiveFlag=1
tx#=GetRawTouchCurrentX(x)
ty#=GetRawTouchCurrentY(x)
// if new touch, then initially position joystick
if TouchEvents[x].JoystickStaked=0
PositionMyVirtualJoyStick(tx#,ty#)
TouchEvents[x].JoystickStaked=1
endif
// just move the actual joystick now (/2.0)
diffx#=getSpriteXbyOffset(JoystickOuter)-tx#
diffy#=getSpriteYbyOffset(JoystickOuter)-ty#
mydistance#=sqrt((diffx#*diffx#)+(diffy#*diffy#)) // total distance
SetSpritePositionByOffset(JoystickInner,tx#,ty#)
endif
next x
endfunction
function PositionMyVirtualJoyStick(x#,y#)
SetSpritePositionByOffset(JoystickOuter,x#,y#)
SetSpritePositionByOffset(JoystickInner,x#,y#)
endfunction