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评论62阅读模式
英文:

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

问题

@tool
extends TouchScreenButton

@export_range(0.0,1.0) var inner_opacity : float = 0.9
@export_range(0.0,1.0) var outer_opacity : float = 0.5

var direction : Vector2 = Vector2(0,0)
var strength : float = 0.0
var was_pressed : bool = false
var button_index : int = -1
var held : bool = false

@onready var half_width := self.texture_normal.get_width()/2
@onready var half_height := self.texture_normal.get_height()/2
@onready var inner_joystick_image : Sprite2D = $inner
@onready var radius :float = 75.0


signal joystick_input
signal joystick_released

func _ready():
	inner_joystick_image.position = Vector2(half_width,half_height)
	self_modulate = Color(1,1,1,outer_opacity)	
	$inner.modulate = Color(1,1,1,inner_opacity)
	$inner.position = Vector2(half_width,half_height)
	if shape is CircleShape2D:
		radius = shape.radius


func _input(event):
	if not event is InputEventScreenTouch and not event is InputEventScreenDrag: # Not a touch
		return

	if button_index != -1 and button_index != event.index: # No index, or index 
		return
	if event is InputEventScreenTouch and event.pressed == false:
		inner_joystick_image.global_position = global_position + Vector2(half_width,half_height)
		button_index = -1
		emit_signal("joystick_input", 0, Vector2(0,0), 0)
		emit_signal("joystick_released")
		held = false
		return
	if is_pressed(): # Button still in pressed state
		strength = event.position.distance_to(global_position+Vector2(half_width,half_height))
		if strength <= radius:
			button_index = event.index
		if button_index != event.index:
			return
		strength = smoothstep(0,radius,strength)
		direction = event.position.direction_to(global_position+Vector2(half_width,half_height)) * -1
		inner_joystick_image.global_position = clamp_to_circle(global_position+Vector2(half_width,half_height), radius, event.position)
		get_viewport().set_input_as_handled()
		held = true


func clamp_to_circle(point: Vector2, radius: float, value: Vector2) -> Vector2:
	var direction = value - point
	if direction.length_squared() > radius * radius:
		direction = direction.normalized() * radius
	return point + direction
	
func _process(delta):
	if held:
		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:
"

@tool
extends TouchScreenButton
@export_range(0.0,1.0) var inner_opacity : float = 0.9
@export_range(0.0,1.0) var outer_opacity : float = 0.5
var direction : Vector2 = Vector2(0,0)
var strength : float = 0.0
var was_pressed : bool = false
var button_index : int = -1
var held : bool = false
@onready var half_width := self.texture_normal.get_width()/2
@onready var half_height := self.texture_normal.get_height()/2
@onready var inner_joystick_image : Sprite2D = $inner
@onready var radius :float = 75.0
signal joystick_input
signal joystick_released
func _ready():
inner_joystick_image.position = Vector2(half_width,half_height)
self_modulate = Color(1,1,1,outer_opacity)	
$inner.modulate = Color(1,1,1,inner_opacity)
$inner.position = Vector2(half_width,half_height)
if shape is CircleShape2D:
radius = shape.radius
func _input(event):
if not event is InputEventScreenTouch and not event is InputEventScreenDrag: # Not a touch
return
if button_index != -1 and button_index != event.index: # No index, or index 
return
if event is InputEventScreenTouch and event.pressed == false:
inner_joystick_image.global_position = global_position + Vector2(half_width,half_height)
button_index = -1
emit_signal(&quot;joystick_input&quot;, 0, Vector2(0,0), 0)
emit_signal(&quot;joystick_released&quot;)
held = false
return
if is_pressed(): # Button still in pressed state
strength = event.position.distance_to(global_position+Vector2(half_width,half_height))
if strength &lt;= radius:
button_index = event.index
if button_index != event.index:
return
strength = smoothstep(0,radius,strength)
direction = event.position.direction_to(global_position+Vector2(half_width,half_height)) * -1
inner_joystick_image.global_position = clamp_to_circle(global_position+Vector2(half_width,half_height), radius, event.position)
get_viewport().set_input_as_handled()
held = true
func clamp_to_circle(point: Vector2, radius: float, value: Vector2) -&gt; Vector2:
var direction = value - point
if direction.length_squared() &gt; radius * radius:
direction = direction.normalized() * radius
return point + direction
func _process(delta):
if held:
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。要做到这一点,最好的方式可能是在物理过程循环中。

func _physics_process(delta):
    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.

func _physics_process(delta):
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:

确定