2012-01-03 71 views
1

为了构建一个用于测试的井字游戏,我有以下例程。但问题是,只有一次触碰我就会得到太多的事件。我怀疑isTouched()会返回所有的下移,上移和移动。有什么方法可以举办活动吗?从LIBGDX获取不必要的触摸事件

更新:通过使用justTouched()来解决此问题。

@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; 
    } 

} 

回答

1

使用justTouched的替代方法是实现InputProcessor接口,因为它有一个触上(X,Y,指针,按钮),其可以提供对输入更大的控制。有几个类可以实现它,或者你可以让你的类实现它。

0

您可以创建一个电路板(例如哈希映射),并且您的游戏中的每个对象都希望可以点击添加自己到该电路板,如果一个对象被触及并且在板上它将捕捉事件。如果没有,它不会赶上事件。太简单! :)