首页 > 解决方案 > 如何判断用户是否释放了滑块

问题描述

在 github 上存在一个问题,向滑块节点添加released() 信号,但是没有它我怎么做同样的事情呢?

我想要一个滑块,当用户移动它时,它会在屏幕上的标签上显示“Value is now X”。但是,当我根据“value_changed(x)”执行此操作时,它会在拖动滑块时多次调用。我希望它在播放器在滑动后释放时设置我唯一的标签,或者在不使用抓取器的情况下按下并释放滑块范围上的区域以选择新值时。

标签: godot

解决方案


You can achieve what you want by overriding the _gui_input function. Attach a script to your slider, and then add this code:

func _gui_input(event):
    if (event is InputEventMouseButton) && !event.pressed && (event.button_index == BUTTON_LEFT):
        print("Released")

This will work whether the user releases the grabber or "releases an area on the slider's range to select a new value without using the grabber", and achieves what you want. However, if the code is meant to run on a device with a keyboard (e.g. a PC), then the user can also change the value via the cursor keys on the keyboard, and you may want to add support for that too.


推荐阅读