Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

getting unnecessary touch events from LIBGDX

In order to build a tic-tac-toe game for testing, I have following routine. But problem is that I am getting too many events for just one touch. I suspect isTouched() returns all of down, up, and move. Is there any way to just get up event?

UPDATE: Resolved the issue by employing justTouched() instead.

@Override
public void render() {
    // we update the game state so things move.
    updateGame();

    // First we clear the screen
    GL10 gl = Gdx.graphics.getGL10();
    gl.glViewport(0, 0, width, height);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    // Next we update the camera and set the camera matrix
    camera.update();
    camera.apply(Gdx.gl10);


    ...       
}
private void updateGame() {
    // the delta time so we can do frame independant time based movement
    float deltaTime = Gdx.graphics.getDeltaTime();


    // Has the user touched the screen? then position the paddle
    if (Gdx.input.isTouched() && !isProcess) {
        // get the touch coordinates and translate them
        // to the game coordinate system.
        isProcess=true;
        int width = Gdx.graphics.getWidth();
        int height = Gdx.graphics.getHeight();
        int offx=-width/2;
        int offy=-height/2;
        float x = Gdx.input.getX();
        float y = Gdx.input.getY();
        float touchX = 480 * (x
                / (float) width - 0.5f);
        float touchY = 320 * (0.5f - y
                / (float) height);
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++)
            {
                if(touchX >= offx+i*width/3 && touchX < offx+(i+1)*width/3 &&
                        touchY >= offy+j*height/3 && touchY < offy+(j+1)*height/3)
                {
                    if(isCurrentO)
                        data[i][j]=CellStatus.O;
                    else
                        data[i][j]=CellStatus.X;
                    isCurrentO=!isCurrentO;
                    break;
                }
            }
        }
        isProcess=false;
    }

}

Upvotes: 1

Views: 2206

Answers (2)

Hamed Nova
Hamed Nova

Reputation: 1081

You can create a board for example (with hash map) and each object in your game wants to be clickable add itself to that board if an object was touched and was in board it will catch the event. If not it will not catch the event. So easy! :)

Upvotes: 0

Doran
Doran

Reputation: 4091

An alternative to using justTouched is to implement the InputProcessor interface, as it has a touchUp(x,y,pointer,button) which gives you greater control over the input. There are several classes that implement this or you can have your class implement it.

Upvotes: 1

Related Questions