![](/img/trans.png)
[英]libgdx touchup and touchdragged not being called, but touchdown is
[英]LibGDX touchDown touchUp on different Actors
在我的 LibGDX 游戲中,我有一個可點擊的 Actor 的光柵。 我希望玩家能夠在它們上滑動並在滑動停止的地方獲得 Actor。
我不想要GestureListener
,我唯一需要的是在另一個 Actor 上生成的 touchDown 上的 touchUp 事件。 例如:想想 Android 登錄屏幕,您可以在其中滑動一個圖案來解鎖,但我只需要最后一個點/Actor 的事件。
我曾嘗試使用touchDown
/ touchUp
,但事件總是在發生touchDown
的 Actor 上touchDown
。
一個很好的解決方案是獲取滑動擊中的每個 Actor 的事件,然后獲取全局touchUp
事件。 這是否可能以一種相對易於訪問的方式 - 例如。 無需編寫全新的 InputListener?
在您的監聽器的touchUp()
中,在所有可點擊 Actor 的父 WidgetGroup 上調用hit
以查看手指在松開時位於哪一個上。 請注意,由於您將演員添加到組中,因此需要將組添加到舞台而不是直接將小部件添加到舞台。 請記住,Actor 的 X 和 Y 是相對於其父級的。
final WidgetGroup parent = new WidgetGroup();
InputListener inputListener = new InputListener(){
private final Vector2 tmp = new Vector2();
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
event.getTargetActor().localToParentCoordinates(tmp.set(x, y));
Actor releaseOverActor = parent.hit(tmp.x, tmp.y, true);
if (releaseOverActor != null){
//doSomethingTo(releaseOverActor);
}
}
}
for (Actor clickable : clickableActors){
parent.add(clickable);
clickable.addListener(inputListener);
}
以上稍微簡化了一些。 例如,您可能只想在使用指針 0 時返回 true one touchDown
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.