here is the tutorial video:
Joystick Code
var start_pos: Vector2 = Vector2.ZERO
var end_pos: Vector2 = Vector2.ZERO
var valid_pos = false
onready var js_pos = get_node("background").rect_position
onready var js_bg = get_node("background")
onready var js_handle = get_node("background/handle")
signal swipe_detect(swipe_direction, strength)
signal swipe_end
func _input(event: InputEvent) -> void:
if valid_pos:
if event is InputEventScreenDrag:
if start_pos == Vector2.ZERO:
start_pos = event.position
js_bg.rect_position = Vector2(start_pos.x - 128, start_pos.y - 128)
end_pos = event.position
var direction = end_pos - start_pos
if direction.length() < 120:
js_handle.rect_position.x = 96 + direction.length()
js_handle.rect_pivot_offset.x = 32 - direction.length()
js_handle.rect_rotation = rad2deg(direction.angle())
emit_signal("swipe_detect",direction, direction.length())
func _on_TextureButton_button_down() -> void:
valid_pos = true
func _on_TextureButton_button_up() -> void:
emit_signal("swipe_end")
js_bg.rect_position = js_pos
start_pos = Vector2.ZERO
end_pos = Vector2.ZERO
valid_pos = false
js_handle.rect_position.x = 96
js_handle.rect_pivot_offset.x = 32
Players Code
var direction = Vector2.ZERO
var speed = 300
func _process(delta: float) -> void:
move_and_slide(direction * speed)
func _on_joystick_swipe_detect(swipe_direction, strength) -> void:
direction = swipe_direction.normalized()
$".".rotation_degrees = rad2deg(swipe_direction.angle())
if strength < 300:
speed = strength
func _on_joystick_swipe_end() -> void:
direction = Vector2.ZERO
Comments
Post a Comment