触摸发布前后获取坐标

时间:2013-12-12 11:33:54

标签: java android android-ndk libgdx

我正在尝试使用LIBGDX(目前正在学习)制作弹弓式物品(如愤怒的小鸟)。那么,如何获取用户触摸屏幕的点的坐标并将手指拖动到释放的其他点n。我想要两个坐标的初始点和释放点。

我也想知道如何在Android SDK / NDK中实现上述功能。

3 个答案:

答案 0 :(得分:2)

我没有测试过这个,但这应该是关于你需要的东西

@Override
Public boolean onTouchEvent (MotionEvent event) {
int downx;
int downy;
int upx;
int upy;
switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
       downy = (int)event.gety();
       downx = (int)event.getx();
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
         upy = (int)event.gety();
         upx = (int)event.getx();
}
return false;
}

答案 1 :(得分:0)

public class TempScreen extends Screen
{

Game game; // the class which is implementing the InputProcessor
SpriteBatch batcher;
Vector3 touchPoint;
OrthographicCamera cam;
InputMultiplexer multiplexer;
GestureListener listener=new GestureListener()
{

    @Override
    public boolean zoom(float initialDistance, float distance)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(float x, float y, int pointer, int button)
    {
        cam.unproject(touchPoint.set(x, y, 0));
        System.out.println(touchPoint);
        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
            Vector2 pointer1, Vector2 pointer2)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean longPress(float x, float y)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean fling(float velocityX, float velocityY, int button)
    {
        // TODO Auto-generated method stub
        return false;
    }
};

GestureDetector detector=new GestureDetector(listener)
{
    public boolean touchUp(float x, float y, int pointer, int button) 
    {
        cam.unproject(touchPoint.set(x, y, 0));
        System.out.println(touchPoint);

        return false;
    }
};
public TempScreen(Game game,SpriteBatch batcher)
{
    super(game);
    this.batcher=batcher;
    this.game=game;
    touchPoint = new Vector3();
    cam = new OrthographicCamera(GameConstants.CAMERA_WIDTH,GameConstants.CAMERA_HEIGHT);
    cam.position.set(GameConstants.CAMERA_WIDTH / 2,GameConstants.CAMERA_HEIGHT / 2, 0);
    multiplexer = new InputMultiplexer();
    multiplexer.addProcessor(game);
    multiplexer.addProcessor(detector);
    Gdx.input.setInputProcessor(multiplexer);

}

@Override
public void render(float deltaTime)
{
    update(deltaTime);
    GLCommon gl = Gdx.gl;
    gl.glClearColor(0, 0f, 1f, 0.1f);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    cam.update();
    batcher.setProjectionMatrix(cam.combined);
    batcher.enableBlending();
    batcher.begin();
    draw(deltaTime);
    batcher.end();
}

@Override
public void draw(float deltaTime)
{
    // TODO Auto-generated method stub

}

@Override
public void update(float deltaTime)
{
    // TODO Auto-generated method stub

}

@Override
public void backKeyPressed()
{
    // TODO Auto-generated method stub

}

}

答案 2 :(得分:0)

创建一个InputProcessor和一个Vector3:

MyInputprocessor myInputprocessor;
Vector3 touchPoint = new Vector3();

像这样定义InputProcessor:

public class MyInputprocessor implements InputProcessor{

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button){
        guicam.unproject(touchPoint.set(screenX, screenY, 0)); //Initial point coordinates
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer){
        guicam.unproject(touchPoint.set(screenX, screenY, 0)); //current point coordinates (when you are dragging it)
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button){
        guicam.unproject(touchPoint.set(screenX, screenY, 0)); //final/release point coordinates
        return false;
    }

    @Override public boolean keyTyped(char character){return false;}
    @Override public boolean mouseMoved(int screenX, int screenY){return false;}
    @Override public boolean scrolled(int amount){return false;}
    @Override public boolean keyDown(int keycode){return false;}
    @Override public boolean keyUp(int keycode){return false;}
}

创建它并在你的ini代码中设置它:

myInputprocessor = new MyInputprocessor();
Gdx.input.setInputProcessor(myInputprocessor);