Timer 节点中的 time_left 函数不工作,“无效调用基 'Timer' 中不存在函数 'time_left'”

问题描述

extends Area2D

export var rot_speed = 2.6
export var thrust = 500
export var max_vel = 400
export var friction = 0.65

export (PackedScene) var bullet
onready var bullet_container = get_node("BulletContainer")
onready var gun_timer = get_node("gunTimer")


var screen_size
var rot = 0
var pos = Vector2()
var acc = Vector2()
var vel = Vector2()

func _ready():
    screen_size = get_viewport_rect().size
    pos = screen_size / 2
    position = pos

func _process(delta: float) -> void:
    if Input.is_action_pressed("player_shoot"):
        if gun_timer.time_left() == 0:
        shoot()
    if Input.is_action_pressed("player_left"):
        rot -= rot_speed * delta
    if Input.is_action_pressed("player_right"):
        rot += rot_speed * delta
    if Input.is_action_pressed("player_thrust"):
        acc = Vector2(0,-thrust).rotated(rot)
    else:
        acc = Vector2(0,0)

    acc -= vel*friction
    vel += acc * delta
    pos += vel * delta

    if pos.x >= screen_size.x:
        pos.x = 0
    if pos.x < 0:
        pos.x = screen_size.x
    if pos.y >= screen_size.y:
        pos.y = 0
    if pos.y < 0:
        pos.y = screen_size.y


    position = pos

    rotation = rot

func shoot():
    gun_timer.start()
    var b = bullet.instance()
    bullet_container.add_child(b)
    b.start_at(rotation,get_node("bullet_spawn_pos").global_position)

这是我的播放器的脚本。当我输入射击时,我的播放器正在射击子弹,但我想要一个计时器来限制子弹可以发射的速度。所以我创建了一个计时器节点,但是当我调用 time_left 函数时,我收到一条错误消息,指出没有这样的函数“无效调用。基本‘Timer’中不存在函数‘time_left’。”查看 if 语句第 26 行。

我尝试从不同的地方调用函数,但我总是收到错误消息。我是 Godot 的新手,希望得到任何帮助。

解决方法

您可以使用 timer.is_stopped() 并且您可以在输入行 add 和 timer.is_stopped() 上使用它,因此当计时器为零时您可以再次射击,但这也意味着您必须告诉它开始输入函数中的定时器。

还要确保计时器是一次性计时器