布尔值只工作一次然后停止

boolean value only works once and then stops

所以我正在使用 godot 并且我试图设置一个按钮来在全屏和非全屏之间切换,当我 运行 它并单击按钮时它切换到全屏但随后它决定不想切换返回
代码如下:


extends Button

var on = false

func _on_FullScreen_pressed():
    if on == true:
        on = false
        OS.window_fullscreen = false
        $Label.text = "Fullscreen: No"

    if on == false:
        on = true
        OS.window_fullscreen = true
        $Label.text = "Fullscreen: Yes"

我假设问题正在发生,因为我在顶部的 if 语句中设置为 false,然后在底部的 if 语句中将其读取为 false 并将其设置回
如果是这种情况,请为我提供解决方法

把第二个if改成elif。或者 else 并删除条件

extends Button

var on = false

func _on_FullScreen_pressed():
    if on == true:
        on = false
        OS.window_fullscreen = false
        $Label.text = "Fullscreen: No"

    else:
        on = true
        OS.window_fullscreen = true
        $Label.text = "Fullscreen: Yes"

您可以使用这样的切换:

 extends Button

var fullscreen = false

func _on_FullScreen_pressed():
    fullscreen = !fullscreen #toggles fullscreen
    OS.window_fullscreen = fullscreen
    if(fullscreen):
        $Label.text = "Fullscreen: Yes"
    else:
        $Label.text = "Fullscreen: No"

或者更好的是,使用带有正确事件的可切换按钮:

extends Button

func _ready():
    set_toggle_mode(1)

func _on_Button_toggled(button_pressed):
    OS.window_fullscreen = button_pressed
    if button_pressed:
        $Label.text = "Fullscreen: Yes"
    else:
        $Label.text = "Fullscreen: Yes"