首页
学习
活动
专区
圈层
工具
发布

Godot函数
EN

Stack Overflow用户
提问于 2022-10-07 18:25:16
回答 2查看 44关注 0票数 0

因此,我为一个破折号函数编写的代码不能正常工作,尽管我肯定逻辑是正确的。我怀疑这个问题是变量isdashing的问题,所以我打印出了它的值,不管我做了什么,都会返回错误的结果。有人能告诉我我做错了什么吗?

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2022-10-25 02:27:59

Input.is_action_just_pressed("ui_Dash")很可能是假的,因为dash()是在physics_process函数中调用的,该函数不运行每个帧(它每秒运行60次)。

目前发生的事情是-

  1. 在游戏帧中,用户按下破折号按钮。此时,Input.is_action_just_pressed("ui_Dash")为真,但没有进行检查。
  2. 一个或两个(或更多)后,在physics_process中调用dash函数。如果刚刚按下了破折号按钮,这就会进行投票--但现在它是错误的,因为它是在几个帧之前“按下”的,而不是在这个帧上。

当前逻辑工作的唯一方法是,如果按下了physics_process运行的精确框架中的dash按钮。

您可以通过使用Input.is_action_pressed("ui_Dash")来捕获按钮按下,设置一个标志以防止在下一个物理框架中捕获它,然后稍后再启用它。例如。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-11-02 21:55:43

您正在重置变量(特别是速度),然后调用move_and_slide()。

解决方案:在调用move_and_slide()之前立即调用dash()。

这就是我所指的重置:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73991290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档