每当我跳跃时,如何阻止我的角色向右跳跃?

how to stop my character from jumping right whenever I just jump?

我正在开发一款 2d 横版卷轴游戏,我 运行 在尝试对玩家角色进行射击时遇到了问题,过去是在跳跃时射击但现在不会射击每当我按下跳跃键时,即使我没有激活方向键,玩家也会向右跳跃

这里是代码

extends KinematicBody2D

const GRAVITY = 20
const SPEED = 200
const JUMP_HIGHT = -550
const UP = Vector2(0,-1)

const SHOOT = preload("res://shoot.tscn")

var motion = Vector2()
var on_ground = false
var is_attacking = false

# warning-ignore:unused_argument
func _physics_process(delta: float) -> void:
    motion.y += GRAVITY

    if Input.is_action_pressed("right") || is_on_floor() == false:
        if is_attacking == false:
            motion.x = SPEED
            if is_attacking == false:
                $Sprite.flip_h = false
                $Sprite.play("run")
                if sign($Position2D.position.x) == -1:
                    $Position2D.position.x *= -1

    elif Input.is_action_pressed("left") || is_on_floor() == false:
        if is_attacking == false :
            motion.x = -SPEED
            if is_attacking == false:
                $Sprite.flip_h = true
                $Sprite.play("run")
                if sign($Position2D.position.x) == 1:
                    $Position2D.position.x *= -1

    else : 
        if on_ground == true && is_attacking == false :
            $Sprite.play("idle")
            motion.x = 0

    
    if Input.is_action_just_pressed("jump"):
        if is_attacking == false :
            if on_ground == true :
                    motion.y = JUMP_HIGHT
                    on_ground = false

    if is_on_floor():
        if on_ground == false :
            is_attacking = false
        on_ground = true
    else :
        if is_attacking == false :
            on_ground = false
            if motion.y < 0 :
                $Sprite.play("jump")
            else :
                $Sprite.play("fall")

    if Input.is_action_just_pressed("shoot") && is_attacking == false:
        if is_on_floor() :
            motion.x = 0
        is_attacking = true
        $Sprite.play("attack")
        var shoot = SHOOT.instance()
        if sign($Position2D.position.x) == 1 :
            shoot.set_shoot_direction(1)
        else:
            shoot.set_shoot_direction(-1)
        
        get_parent().add_child(shoot)
        shoot.position = $Position2D.global_position
        
    motion = move_and_slide(motion,UP)
    

func _on_Sprite_animation_finished() -> void:
    is_attacking = false

不确定这是否有帮助,但对于我的代码,我必须将 and ev.pressed and !ev.is_echo() 添加到我的所有 if Input 语句中。不太清楚为什么,但它阻止了程序随机认为我试图按下我不想按下的键。

我认为您从一开始就走错了方向,而且我在教程中还没有看到任何人这样做。您反复进行输入检查以及状态检查和移动。由于您是新手,因此您需要多次从头开始编写平台游戏代码来进行实验,并了解代码应该如何流动。 分开做:

# don't use $ for nodes you call often save reference in variable (I'm guessing it's AnimatedSprite)
var sprite:AnimatedSprite = $Sprite
var dir:float = 0.0 #used for movement and sprite flipping
#Limit your code to physics process only for physics related logic
func _physics_process(delta:float)->void:
    # get direction input
    # math trick to get value -1 to 1 to know how strong go which direction
    dir = Input.get_action_strength("move_right") - 
    Input.get_action_strength("move_left")

    #ground check - call function once and use variable
    is_grounded = is_on_floor()

    # apply speed(use dir to give direction)
    motion.x = SPEED * dir

    # apply gravity
    if !is_grounded:
        motion.y += GRAVITY * delta
    elif Input.is_action_just_pressed("jump"):
        motion.y = JUMP_HIGHT

    # apply movement
    motion = move_and_slide(motion, Vector2.UP)

# use process for sprites
func _process(delta:float)->void:
    #check if direction is not close to 0
    if abs(dir) > 0.001:
        if dir > 0:
            sprite.h_flip = false
        else:
            sprite.h_flip = true
    
    #state check for sprites
    if is_grounded:
        if abs(dir) > 0.001:
            sprite.play("run")
        else:
            sprite.play("idle")
    else:
        if motion.y < 0.0:
            sprite.play("jump")
        else:
            sprite.play("fall")

这将是实施射击的更好起点。好像你在射击时停止了移动,所以你可以在应用速度之前中断方向。

   if is_shooting && is_grounded:
       dir = 0.0