英文:
libGDX DragAndDropText.java is broken, looking for another example
问题
我想要将 TextButtons 拖放到指定的目标上。在搜索这个网站时,引用了 libgdx 的测试 GitHub 上的 DragAndDropTest.java 作为一个开始的示例。实施测试中描述的流程时,我收到了这个错误:
Exception in thread "main" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop$1.drag(DragAndDrop.java:111)
at com.badlogic.gdx.scenes.scene2d.utils.DragListener.touchDragged(DragListener.java:61)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:62)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDragged(Stage.java:315)
at com.badlogic.gdx.InputEventQueue.drain(InputEventQueue.java:89)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Input.update(Lwjgl3Input.java:205)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:390)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:137)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:111)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.createApplication(Lwjgl3Launcher.java:17)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.main(Lwjgl3Launcher.java:12)
最终,我尝试直接运行 DragAndDropTest.java,也收到了相同的错误。无论是来自一个修复后的测试,指出我在尝试运行测试时可能出错的地方,还是一个不同的可以供我学习的工作中的拖放实例,都将有助于我继续前进。
需要注意的是,当我在实施 Lee Stemkoski 的 DragAndDropActor 类时,我尝试了另一种方法。虽然这样做是可以的,但我从中学到的流程只适用于他的 BaseActor 类型,而在其上重新实现 Buttons 等内容似乎不是正确的解决方案。从那个尝试中,我可以确认拖放本身并没有问题,只是测试出现了问题。
英文:
I want to drag and drop TextButtons to designated targets. Searching this site, DragAndDropTest.java on libgdx's testing github is cited as an example to to start from. Implementing the flow described in the test, I received this error:
Exception in thread "main" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop$1.drag(DragAndDrop.java:111)
at com.badlogic.gdx.scenes.scene2d.utils.DragListener.touchDragged(DragListener.java:61)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:62)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDragged(Stage.java:315)
at com.badlogic.gdx.InputEventQueue.drain(InputEventQueue.java:89)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Input.update(Lwjgl3Input.java:205)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:390)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:137)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:111)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.createApplication(Lwjgl3Launcher.java:17)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.main(Lwjgl3Launcher.java:12)
Eventually I just tried to run the DragAndDropTest.java directly and received the same error. Anything from a fixed test, pointing out where I may have gone wrong in trying to run the test, or a different working example of drag and drop to study would all be helpful in moving forward.
Note that I also tried a different path when I struck out here in implementing Lee Stemkoski's DragAndDropActor class. While that did work, the flow I learned from doing so is only good for his BaseActor family, and the work of re-implementing Buttons and such on top of it doesn't seem like the right solution. I'll take from that effort confirmation that drag and drop itself is not broken, just the test.
答案1
得分: 0
我已经从Stemkoski的DragAndDropActor中删除了所有的拖动方法和跟踪变量,并将它们放在.addListener()中,用于我想在屏幕内拖动的每个项。现在,我已经看到了我的可拖动项在屏幕上移动而没有崩溃,我想我会创建一个新的DragListener,它继承自InputListener,因为这似乎是封装逻辑的方式(欢迎反馈)。无论如何,我注意到在DragAndDropText中找到的DragAndDrop类在Stemkoski的作品中从未被引用过,所以他必须实现了自己的并行拖放功能。所以,我收回我关于了解原生libgdx附带的拖放功能的说法,尽管它似乎不太可能不起作用。
虽然从技术上讲,这仍然是一个与常被引用的测试类相比仍然有效的拖放示例,但我认为任何提供使用原始libgdx软件包的答案都应该被视为更优的答案。
英文:
I've ripped out all of the dragging methods and tracking variables from Stemkoski's DragAndDropActor, and placed them in .addListener() for each item I want to drag within my screen. Now that I've seen my draggables moving around the screen without crashing, I think I'll make a new DragListener extending from InputListener, as that seems like the way to encapsulate the logic (open to feedback). Anyway, it occurs to me that the DragAndDrop class found in the DragAndDropText is never referenced in Stemkoski's work, so he must have implemented his own parallel drag and drop functionality. So I take back what I said about knowing the drag and drop that comes with native libgdx works, however unlikely it would seem that it not.
While this is technically an example of drag and drop that still works compared to the oft-cited test class, I think any answer provided that uses the vanilla libgdx packages should be considered a superior one.
答案2
得分: 0
我做过类似的事情:
public class DragAndDropActorListener extends DragListener {
private Actor actor;
public DragAndDropActorListener(Actor actor) {
this.actor = actor;
}
@Override
public void drag(InputEvent event, float x, float y, int pointer) {
this.actor.setY(event.getStageY());
this.actor.setX(event.getStageX());
super.drag(event, x, y, pointer);
}
}
通过使用 actor 对象来移动。你也可以为了让对象在鼠标上居中,加入一个小的偏移量。
英文:
I've done something like that :
public class DragAndDropActorListener extends DragListener {
private Actor actor;
public DragAndDropActorListener(Actor actor) {
this.actor = actor;
}
@Override
public void drag(InputEvent event, float x, float y, int pointer) {
this.actor.setY(event.getStageY());
this.actor.setX(event.getStageX());
super.drag(event, x, y, pointer);
}
}
With actor the object to move. you could also a little Offset for center the object on the mouse
专注分享java语言的经验与见解,让所有开发者获益!
评论