第一次在这里发帖,所以我希望能让自己清楚。 正如标题所述,我正在创建一个触摸板,弹出用户触摸设备屏幕的位置。触摸板在前面创建,并移动到触摸事件的X,Y坐标。现在实现它的方式是触摸板在移动之后不会像触摸那样表现,因为在触摸板位于该位置之前手指在屏幕上。结果是您必须先双击才能使用它(首先点击移动它,第二次点击开始使用它)。有没有办法解决这个问题,例如通过发射第二个"人工"触摸事件在同一位置?如果可能,请提供代码示例。
这是我的重新定位代码:
if(popupMode) {
// Adjust the touchpad position when in popup mode
if(Gdx.input.isTouched()) {
// Assume at most 2 fingers touch the screen simultaneously
boolean left, right;
left = false;
right = false;
int x0, x1, y0, y1;
x0 = -1;
x1 = -1;
y0 = -1;
y1 = -1;
if(Gdx.input.isTouched(0)) {
x0 = Gdx.input.getX(0);
y0 = Gdx.input.getY(0);
if(x0 < (Gdx.graphics.getWidth() / 2.0f))
left = true;
else
right = true;
}
if(Gdx.input.isTouched(1)) {
x1 = Gdx.input.getX(1);
y1 = Gdx.input.getY(1);
if(x1 < (Gdx.graphics.getWidth() / 2.0f))
left = true;
else
right = true;
}
if(left) {
if(!repositionedMove) {
if( (x0 > -1) &&
(x0 > (touchpadMove.getWidth() / 2)) &&
(x0 < ((Gdx.graphics.getWidth() / 2) - (touchpadMove.getWidth() / 2))) &&
(y0 > (touchpadMove.getHeight() / 2)) &&
(y0 < (Gdx.graphics.getHeight() - (touchpadMove.getHeight() / 2)))) {
touchpadMove.setX(x0 - (touchpadMove.getWidth() / 2));
touchpadMove.setY((Gdx.graphics.getHeight() - y0) - (touchpadMove.getHeight() / 2));
touchpadMove.layout();
repositionedMove = true;
}
if( (x1 > -1) &&
(x1 > (touchpadMove.getWidth() / 2)) &&
(x1 < ((Gdx.graphics.getWidth() / 2) - (touchpadMove.getWidth() / 2))) &&
(y1 > (touchpadMove.getHeight() / 2)) &&
(y1 < (Gdx.graphics.getHeight() - (touchpadMove.getHeight() / 2)))) {
touchpadMove.setX(x1 - (touchpadMove.getWidth() / 2));
touchpadMove.setY((Gdx.graphics.getHeight() - y1) - (touchpadMove.getHeight() / 2));
touchpadMove.layout();
repositionedMove = true;
}
}
}
else
repositionedMove = false;
if(right) {
if(!repositionedRotate) {
if( (x0 > -1) &&
(x0 > ((touchpadRotate.getWidth() / 2) + (Gdx.graphics.getWidth() / 2))) &&
(x0 < (Gdx.graphics.getWidth() - (touchpadRotate.getWidth() / 2))) &&
(y0 > (touchpadRotate.getHeight() / 2)) &&
(y0 < (Gdx.graphics.getHeight() - (touchpadRotate.getHeight() / 2)))) {
touchpadRotate.setX(x0 - (touchpadRotate.getWidth() / 2));
touchpadRotate.setY((Gdx.graphics.getHeight() - y0) - (touchpadRotate.getHeight() / 2));
touchpadRotate.layout();
repositionedRotate = true;
}
if( (x1 > -1) &&
(x1 > ((touchpadRotate.getWidth() / 2) + (Gdx.graphics.getWidth() / 2))) &&
(x1 < (Gdx.graphics.getWidth() - (touchpadRotate.getWidth() / 2))) &&
(y1 > (touchpadRotate.getHeight() / 2)) &&
(y1 < (Gdx.graphics.getHeight() - (touchpadRotate.getHeight() / 2)))) {
touchpadRotate.setX(x1 - (touchpadRotate.getWidth() / 2));
touchpadRotate.setY((Gdx.graphics.getHeight() - y1) - (touchpadRotate.getHeight() / 2));
touchpadRotate.layout();
repositionedRotate = true;
}
}
}
else
repositionedRotate = false;
}
else {
repositionedMove = false;
repositionedRotate = false;
}
}
答案 0 :(得分:2)
我前一段时间遇到过这个问题。为了解决这个问题,我使用了人工触摸事件方法。
简而言之,你必须:
同样,当您检测到屏幕不再被触摸时,则隐藏触摸板。
package com.badlydrawngames.wf.controllers;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputEvent.Type;
public class FloatingThumbpadController extends FixedThumbpadController {
private Vector2 screenPos;
private Vector2 localPos;
private InputEvent fakeTouchDownEvent;
public FloatingThumbpadController() {
screenPos = new Vector2();
localPos = new Vector2();
fakeTouchDownEvent = new InputEvent();
fakeTouchDownEvent.setType(Type.touchDown);
}
@Override
public Vector2 getDirection() {
if (Gdx.input.justTouched()) {
// Get the touch point in screen coordinates.
screenPos.set(Gdx.input.getX(), Gdx.input.getY());
// Convert the touch point into local coordinates, place the touchpad and show it.
localPos.set(screenPos);
localPos = touchpad.getParent().screenToLocalCoordinates(localPos);
touchpad.setPosition(localPos.x - touchpad.getWidth() / 2, localPos.y - touchpad.getHeight() / 2);
touchpad.setVisible(true);
// Fire a touch down event to get the touchpad working.
Vector2 stagePos = touchpad.getStage().screenToStageCoordinates(screenPos);
fakeTouchDownEvent.setStageX(stagePos.x);
fakeTouchDownEvent.setStageY(stagePos.y);
touchpad.fire(fakeTouchDownEvent);
}
else if (!Gdx.input.isTouched()) {
// The touch was just released, so hide the touchpad.
touchpad.setVisible(false);
}
return super.getDirection();
}
}