I'm stuck on a simple drag-and-drop functionality using Kotlin JS.
(我只能使用Kotlin JS进行简单的拖放功能。)
I add a handleDrag
event listener and then I want to remove it in handleMouseUp
listener.
(我添加了handleDrag
事件侦听器,然后在handleMouseUp
侦听器handleMouseUp
其删除。)
That works perfectly.(那很好。)
The thing is that handleMouseUp
also needs to remove itself.
(问题是handleMouseUp
也需要删除自身。)
It's quite straightforward in JS but not so much in Kotlin apparently.(在JS中,这非常简单,但在Kotlin中却没有那么多。)
val handleMouseUp = fun(evt: Event) {
evt as MouseEvent
GUI.removeListener("mousemove", handleDrag)
GUI.removeListener("mouseup", handleMouseUp) //unresolved reference: handleMouseUp
}
GUI.addListener("mousemove", handleDrag)
I also tried named functions;
(我还尝试了命名函数;)
that compiles but doesn't remove the listener for some reason (probably two references like ::handleMouseUp
aren't the same object).(可以编译,但由于某种原因不会删除侦听器(可能像::handleMouseUp
这样的两个引用不是同一对象)。)
Is there any way to do this the way I intended at the beginning?
(有什么方法可以按照我一开始的意图进行?)
If not I accept tips how to work around it.(如果没有,我接受如何解决的提示。)
Maybe I'm missing something because I'm not experienced in event-driven and JS programming.(也许我错过了一些东西,因为我没有事件驱动和JS编程方面的经验。)
removeListener
and addListener
are just wrappers
(removeListener
和addListener
只是包装器)
fun addListener(type: String, listener: (Event) -> Unit) {
canvas.canvas.addEventListener(type, listener)
}
fun removeListener(type: String, listener: (Event) -> Unit) {
canvas.canvas.removeEventListener(type, listener)
}
ask by Adam Stafiej translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…