在 github 上存在一个问题,向滑块节点添加released() 信号,但是没有它我怎么做同样的事情呢?
我想要一个滑块,当用户移动它时,它会在屏幕上的标签上显示“Value is now X”。但是,当我根据“value_changed(x)”执行此操作时,它会在拖动滑块时多次调用。我希望它在播放器在滑动后释放时设置我唯一的标签,或者在不使用抓取器的情况下按下并释放滑块范围上的区域以选择新值时。
在 github 上存在一个问题,向滑块节点添加released() 信号,但是没有它我怎么做同样的事情呢?
我想要一个滑块,当用户移动它时,它会在屏幕上的标签上显示“Value is now X”。但是,当我根据“value_changed(x)”执行此操作时,它会在拖动滑块时多次调用。我希望它在播放器在滑动后释放时设置我唯一的标签,或者在不使用抓取器的情况下按下并释放滑块范围上的区域以选择新值时。
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.
好的,这就是我想出的。它不会真正让我知道滑块何时释放,但它会告诉我玩家何时停止编辑滑块。如果您短暂暂停,它仍会发送警报,但这对我的游戏来说没问题。它不会像您只使用 _on_HSlider_value_changed() 那样发送连续警报,这是我想要避免的。
var old = self.value #start value of slider
var timer_on = false
#will be called continuously while editing timer
func editing_slider(new):
#only start a timer, if there isn't one already or you'll have a million
if not timer_on:
#start timer
timer_on = true
yield(get_tree().create_timer(.2), "timeout" )
timer_on = false
#if still editing, re call function
if old != new:
editing_slider(new)
#done editing
else:
print("slider set to " + str(value))
old = new
func _on_HSlider_value_changed(value):
editing_slider(value)
如果您想避免在用户暂停但尚未释放时调用警报,则必须进行某种 InputEvent 检查。