What code should be written for the movement of the sprite in CharacterBody2D if there is already written such code for the joystick:

huangapple go评论99阅读模式
英文:

What code should be written for the movement of the sprite in CharacterBody2D if there is already written such code for the joystick:

问题

  1. @tool
  2. extends TouchScreenButton
  3. @export_range(0.0,1.0) var inner_opacity : float = 0.9
  4. @export_range(0.0,1.0) var outer_opacity : float = 0.5
  5. var direction : Vector2 = Vector2(0,0)
  6. var strength : float = 0.0
  7. var was_pressed : bool = false
  8. var button_index : int = -1
  9. var held : bool = false
  10. @onready var half_width := self.texture_normal.get_width()/2
  11. @onready var half_height := self.texture_normal.get_height()/2
  12. @onready var inner_joystick_image : Sprite2D = $inner
  13. @onready var radius :float = 75.0
  14. signal joystick_input
  15. signal joystick_released
  16. func _ready():
  17. inner_joystick_image.position = Vector2(half_width,half_height)
  18. self_modulate = Color(1,1,1,outer_opacity)
  19. $inner.modulate = Color(1,1,1,inner_opacity)
  20. $inner.position = Vector2(half_width,half_height)
  21. if shape is CircleShape2D:
  22. radius = shape.radius
  23. func _input(event):
  24. if not event is InputEventScreenTouch and not event is InputEventScreenDrag: # Not a touch
  25. return
  26. if button_index != -1 and button_index != event.index: # No index, or index
  27. return
  28. if event is InputEventScreenTouch and event.pressed == false:
  29. inner_joystick_image.global_position = global_position + Vector2(half_width,half_height)
  30. button_index = -1
  31. emit_signal("joystick_input", 0, Vector2(0,0), 0)
  32. emit_signal("joystick_released")
  33. held = false
  34. return
  35. if is_pressed(): # Button still in pressed state
  36. strength = event.position.distance_to(global_position+Vector2(half_width,half_height))
  37. if strength <= radius:
  38. button_index = event.index
  39. if button_index != event.index:
  40. return
  41. strength = smoothstep(0,radius,strength)
  42. direction = event.position.direction_to(global_position+Vector2(half_width,half_height)) * -1
  43. inner_joystick_image.global_position = clamp_to_circle(global_position+Vector2(half_width,half_height), radius, event.position)
  44. get_viewport().set_input_as_handled()
  45. held = true
  46. func clamp_to_circle(point: Vector2, radius: float, value: Vector2) -> Vector2:
  47. var direction = value - point
  48. if direction.length_squared() > radius * radius:
  49. direction = direction.normalized() * radius
  50. return point + direction
  51. func _process(delta):
  52. if held:
  53. emit_signal("joystick_input", strength, direction, delta)
英文:

What code should be written for the movement of the sprite in CharacterBody2D if there is already written such code for the joystick:
"

  1. @tool
  2. extends TouchScreenButton
  3. @export_range(0.0,1.0) var inner_opacity : float = 0.9
  4. @export_range(0.0,1.0) var outer_opacity : float = 0.5
  5. var direction : Vector2 = Vector2(0,0)
  6. var strength : float = 0.0
  7. var was_pressed : bool = false
  8. var button_index : int = -1
  9. var held : bool = false
  10. @onready var half_width := self.texture_normal.get_width()/2
  11. @onready var half_height := self.texture_normal.get_height()/2
  12. @onready var inner_joystick_image : Sprite2D = $inner
  13. @onready var radius :float = 75.0
  14. signal joystick_input
  15. signal joystick_released
  16. func _ready():
  17. inner_joystick_image.position = Vector2(half_width,half_height)
  18. self_modulate = Color(1,1,1,outer_opacity)
  19. $inner.modulate = Color(1,1,1,inner_opacity)
  20. $inner.position = Vector2(half_width,half_height)
  21. if shape is CircleShape2D:
  22. radius = shape.radius
  23. func _input(event):
  24. if not event is InputEventScreenTouch and not event is InputEventScreenDrag: # Not a touch
  25. return
  26. if button_index != -1 and button_index != event.index: # No index, or index
  27. return
  28. if event is InputEventScreenTouch and event.pressed == false:
  29. inner_joystick_image.global_position = global_position + Vector2(half_width,half_height)
  30. button_index = -1
  31. emit_signal(&quot;joystick_input&quot;, 0, Vector2(0,0), 0)
  32. emit_signal(&quot;joystick_released&quot;)
  33. held = false
  34. return
  35. if is_pressed(): # Button still in pressed state
  36. strength = event.position.distance_to(global_position+Vector2(half_width,half_height))
  37. if strength &lt;= radius:
  38. button_index = event.index
  39. if button_index != event.index:
  40. return
  41. strength = smoothstep(0,radius,strength)
  42. direction = event.position.direction_to(global_position+Vector2(half_width,half_height)) * -1
  43. inner_joystick_image.global_position = clamp_to_circle(global_position+Vector2(half_width,half_height), radius, event.position)
  44. get_viewport().set_input_as_handled()
  45. held = true
  46. func clamp_to_circle(point: Vector2, radius: float, value: Vector2) -&gt; Vector2:
  47. var direction = value - point
  48. if direction.length_squared() &gt; radius * radius:
  49. direction = direction.normalized() * radius
  50. return point + direction
  51. func _process(delta):
  52. if held:
  53. emit_signal(&quot;joystick_input&quot;, strength, direction, delta)

Please help me
Please help me
Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

Please help me

答案1

得分: 2

你最有可能想要移动CharacterBody2D本身,而不是Sprite2D。要做到这一点,最好的方式可能是在物理过程循环中。

  1. func _physics_process(delta):
  2. move_and_collide(direction * strength * delta)
英文:

You'll most likely want to move the CharacterBody2D itself, not the Sprite2D. To do this, the best way is probably in the physics process loop.

  1. func _physics_process(delta):
  2. move_and_collide(direction * strength * delta)

huangapple
  • 本文由 发表于 2023年7月3日 18:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定