因此,我为一个破折号函数编写的代码不能正常工作,尽管我肯定逻辑是正确的。我怀疑这个问题是变量isdashing的问题,所以我打印出了它的值,不管我做了什么,都会返回错误的结果。有人能告诉我我做错了什么吗?
extends KinematicBody2D
export(int) var Jump_Height = -100
export(int) var Jump_Realese = -60
export(int) var gravity = 4
var velocity = Vector2.ZERO
var move_speed = 50
#Jump Stuff
var max_jump = 2
var jump_count = 0
# Dash Stuff
var dash_direction = Vector2(1,0)
var dashable = false
var isdashing = false
# Movement
func _physics_process(delta):
dash()
gravity_control()
if Input.is_action_pressed("ui_right"):
velocity.x = move_speed
elif Input.is_action_pressed("ui_left"):
velocity.x = -move_speed
else:
velocity.x = 0
if is_on_floor() and jump_count != 0:
jump_count = 0
if jump_count<max_jump:
if Input.is_action_just_pressed("ui_up"):
velocity.y = Jump_Height
jump_count += 1
else:
if Input.is_action_just_released("ui_up") and velocity.y < Jump_Realese:
velocity.y = Jump_Realese
velocity = move_and_slide(velocity, Vector2.UP)
func dash():
if is_on_floor():
dashable = true
if Input.is_action_pressed("ui_left"):
dash_direction = Vector2(-1,0)
if Input.is_action_pressed("ui_right"):
dash_direction = Vector2(1,0)
if Input.is_action_just_pressed("ui_Dash") and dashable:
velocity = dash_direction.normalized() * 7000
dashable = false
isdashing = true
yield(get_tree().create_timer(0.2), "timeout")
isdashing = false发布于 2022-10-25 02:27:59
Input.is_action_just_pressed("ui_Dash")很可能是假的,因为dash()是在physics_process函数中调用的,该函数不运行每个帧(它每秒运行60次)。
目前发生的事情是-
当前逻辑工作的唯一方法是,如果按下了physics_process运行的精确框架中的dash按钮。
您可以通过使用Input.is_action_pressed("ui_Dash")来捕获按钮按下,设置一个标志以防止在下一个物理框架中捕获它,然后稍后再启用它。例如。
var can_dash: boolean = true
func dash():
if is_on_floor():
dashable = true
if Input.is_action_pressed("ui_left"):
dash_direction = Vector2(-1,0)
if Input.is_action_pressed("ui_right"):
dash_direction = Vector2(1,0)
if can_dash and Input.is_action_pressed("ui_Dash") and dashable:
can_dash = false;
velocity = dash_direction.normalized() * 7000
dashable = false
isdashing = true
yield(get_tree().create_timer(0.2), "timeout")
isdashing = false
can_dash = true发布于 2022-11-02 21:55:43
您正在重置变量(特别是速度),然后调用move_and_slide()。
解决方案:在调用move_and_slide()之前立即调用dash()。
这就是我所指的重置:
if Input.is_action_pressed("ui_right"):
velocity.x = move_speed
elif Input.is_action_pressed("ui_left"):
velocity.x = -move_speed
else:
velocity.x = 0https://stackoverflow.com/questions/73991290
复制相似问题