Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | pretzldonut |
I am trying to build simple state machine which switches between walking and idling. However when running the script I cannot move my character and can’t quit by pressing ESC. Joystick is a 2D vector for the direction of the arrow keys defined by get_input (). any help on why this may not be working correctly will be aprreciated.
extends KinematicBody2D
func _input(event): #quits on ESC
if event.is_action_pressed("ui_cancel"):
get_tree().quit()
#State Vars
var states = ["idle", "walk"]
var current_state = "idle"
var previous_state = null
const SPEED = 125
#const GRAVITY= 9
#const JUMP_FORCE = -400
var velocity = Vector2(0,0)
var joystick = Vector2(0,0)
const FLOOR = Vector2(0, -1)
func _process(delta):
joystick=get_node("functions").get_input() #gets joysrick input
call(current_state + "_logic", delta)
func set_state(newState : String):
#change values
previous_state = current_state
current_state = newState
func idle_logic(_delta):
if joystick.x != 0:
#set state to walk if you press left or right
set_state("walk")
else:
velocity.x=0
func move_logic():
if joystick.x == 0:
#if no direction pressed set to idle
set_state("idle")
else:
velocity.x=joystick.x*SPEED
func _physics_process(delta):
move_and_slide(velocity,FLOOR)