SetWindowTitle( "Joystick" )
SetWindowSize( 640*0.5, 640*0.5, 0 )
SetVirtualResolution( 640, 640 )
background=LoadSprite('background.jpg')
//--------------------------------------------------------------
type player_class
id as integer
atlas as integer
fps as integer
state_now as string
state_last as string
speed as integer
endtype
global player as player_class
player.id=CreateSprite(0)
player.atlas = LoadImage("player.png")
player.fps = 6
player.speed = 5
SetSpritePositionByOffset(player.id, GetVirtualWidth()/2, GetVirtualHeight()/2-50)
/*
down 1-4
top 5-8
left_right 9-12
left_right_down 13-16
left_right_top 17-20
*/
//--------------------------------------------------------------
//add animation
for i=1 to 20
AddSpriteAnimationFrame(player.id,LoadSubImage(player.atlas, str(i)))
next i
//--------------------------------------------------------------
//create joystick
Joystick = 1
AddVirtualJoystick(Joystick, GetVirtualWidth()/2, GetVirtualHeight()-100,200)
joystick_bg=LoadImage('joystick_bg.png')
joystick_moveable=LoadImage('joystick_moveable.png')
SetVirtualJoystickImageInner(Joystick,joystick_moveable)
SetVirtualJoystickImageOuter(Joystick, joystick_bg )
do
if GetVirtualJoystickX(Joystick) <> 0 or GetVirtualJoystickY(Joystick) <> 0
SetSpritePositionByOffset(player.id, GetSpriteXByOffset(player.id)+GetVirtualJoystickX(Joystick)*player.speed, GetSpriteYByOffset(player.id)+GetVirtualJoystickY(Joystick)*player.speed )
JoystickAngle = GetVirtualJoystickAngle(Joystick)
//top 5-8
if JoystickAngle =>0 and JoystickAngle <= 22
player.state_now='top'
endif
//right_top 17-20
if JoystickAngle => 23 and JoystickAngle <= 67
player.state_now='right_top'
endif
//right 9-12
if JoystickAngle => 68 and JoystickAngle <= 112
player.state_now='right'
endif
//right_down 13-16
if JoystickAngle => 113 and JoystickAngle <= 157
player.state_now='right_down'
endif
//down 1-4
if JoystickAngle => 158 and JoystickAngle <= 202
player.state_now='down'
endif
//left_down 13-16
if JoystickAngle => 203 and JoystickAngle <= 247
player.state_now='left_down'
endif
//left 9-12
if JoystickAngle => 248 and JoystickAngle <= 292
player.state_now='left'
endif
//right_top 17-20
if JoystickAngle => 293 and JoystickAngle <= 337
player.state_now='left_top'
endif
//top 5-8
if JoystickAngle =>338 and JoystickAngle <= 360
player.state_now='top'
endif
//--------------------------------------------------------------
if player.state_now <> player.state_last
select player.state_now
case 'top':
SetSpriteFlip(player.id, 0, 0)
PlaySprite( player.id, player.fps, 1, 5, 8 )
player.state_last='top'
endcase
case 'right_top':
SetSpriteFlip(player.id, 1, 0)
PlaySprite( player.id, player.fps, 1, 17, 20 )
player.state_last='right_top'
endcase
case 'right':
SetSpriteFlip(player.id, 1, 0)
PlaySprite( player.id, player.fps, 1, 9, 12 )
player.state_last='right'
endcase
case 'right_down':
SetSpriteFlip(player.id, 1, 0)
PlaySprite( player.id, player.fps, 1, 13, 16 )
player.state_last='right_down'
endcase
case 'down':
SetSpriteFlip(player.id, 0, 0)
PlaySprite( player.id, player.fps, 1, 1, 4 )
player.state_last='down'
endcase
case 'left_down':
SetSpriteFlip(player.id, 0, 0)
PlaySprite( player.id, player.fps, 1, 13, 16 )
player.state_last='left_down'
endcase
case 'left':
SetSpriteFlip(player.id, 0, 0)
PlaySprite( player.id, player.fps, 1, 9, 12 )
player.state_last='left'
endcase
case 'left_top':
SetSpriteFlip(player.id, 0, 0)
PlaySprite( player.id, player.fps, 1, 17, 20 )
player.state_last='left_top'
endcase
endselect
endif
endif
if GetVirtualJoystickX(Joystick) = 0 and GetVirtualJoystickY(Joystick) = 0
StopSprite( player.id )
player.state_last='stop'
endif
print('State: '+player.state_now)
Print ("JoyStick X: "+Str(GetVirtualJoystickX(Joystick),2))
Print ("JoyStick Y: "+Str(GetVirtualJoystickY(Joystick),2))
print ("JoyStick Angle: "+Str(GetVirtualJoystickAngle(Joystick),0))
Sync()
loop
function GetVirtualJoystickAngle(JoystickID) //offset 0 or 1
x1# = 0
y1# = 0
x2# = GetVirtualJoystickX(JoystickID)
y2# = GetVirtualJoystickY(JoystickID)
result = atanfull (x2# - x1#, y2# - y1#)
endfunction result