Libgdx输入处理器无法正常工作

时间:2014-10-10 04:41:58

标签: java input libgdx touch

我有这个游戏,它遵循玩家在游戏中所做的每一次输入。我为touchDragged,touchDown和touchUp定义了特定的操作。唯一的问题是,无论何时玩家开始移动他的鼠标/手指然后停止但仍然保持他的鼠标/手指,游戏不会捕获该输入,因此游戏无法正常工作

有没有办法处理输入,以防用户拖动手指/鼠标,然后在不抬起手指的情况下停止?

这是我的InputProcessor:

package com.david.helpers;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.david.objects.Acorn;

public class InputHandler implements InputProcessor{

    private Vector2 target;
    private Vector3 temp;
    private OrthographicCamera camera;
    private Array<Acorn> acorns;
    private boolean isAiming = false;

    public InputHandler(Array<Acorn> acorns, OrthographicCamera camera) {
        this.camera = camera;
        this.acorns = acorns;
        target = new Vector2();
        temp = new Vector3();
    }
    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        if(!temp.isZero() && temp != null) {
            target.set(temp.x, temp.y);
            target.sub(acorns.peek().getBody().getPosition());
            target.nor();
            isAiming = true;
        }
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        if(!temp.isZero() && temp != null) {
            target.set(temp.x, temp.y);
            target.sub(acorns.peek().getBody().getPosition());
            target.nor();
            acorns.peek().getBody().setLinearVelocity(target.cpy().scl(25));
            isAiming = false;
        }
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        camera.unproject(temp.set(screenX, screenY, 0));
        if(!temp.isZero() && temp != null) {
            target.set(temp.x, temp.y);
            target.sub(acorns.peek().getBody().getPosition());
            target.nor();
        }
        return true;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }
    public Vector2 getTarget() {
        return this.target;
    }
    public boolean isAiming() {
        return this.isAiming;
    }
}

我背后的问题非常简单 - 我有一个弹丸,在发射之前,用户可以拖动鼠标/手指并选择射击位置。现在,代码根据用户触摸绘制轨迹,并且工作正常。唯一的问题是,无论何时用户停止移动他的手指但仍然保持下来,程序都不会绘制轨迹。在我的Renderer类中,我检查InputProcessor中的isAiming变量是否为true。如果是这样,它会绘制出所需的轨迹。 正如我所看到的那样,它就像InputProcessor在特定时刻没有得到触摸数据,当用户开始在他的手指上移动然后停止他的移动但仍然保持下降时。

这非常烦人,如果你们能帮助我,我会很高兴:)

1 个答案:

答案 0 :(得分:1)

对我而言,Android设备中的触摸通常都是错误的。根据您的需要,请尝试使用Input Polling

//In your render method.
if(Gdx.input.isTouched()){
    camera.unproject(temp.set(Gdx.input.getX(), Gdx.input.getY(), 0));
    if(!temp.isZero() && temp != null) {
        target.set(temp.x, temp.y);
        target.sub(acorns.peek().getBody().getPosition());
        target.nor();
        isAiming = true;
    }
}else{
    if(isAiming){
        camera.unproject(temp.set(Gdx.input.getX(), Gdx.input.getY(), 0));
        if(!temp.isZero() && temp != null) {
            target.set(temp.x, temp.y);
            target.sub(acorns.peek().getBody().getPosition());
            target.nor();
            acorns.peek().getBody().setLinearVelocity(target.cpy().scl(25));
            isAiming = false;
        }
    }
}