0

第一次在这里发帖,所以我希望可以让自己清楚。正如标题所述,我正在创建一个触摸板,它会在用户触摸设备屏幕的地方弹出。触摸板在前面创建并移动到触摸事件的 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;
            }
        } 
4

1 回答 1

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();
    }
}
于 2013-11-05T22:01:07.343 回答