2014-07-16 59 views
1

幾乎我希望我的紋理移動到屏幕上的隨機位置,當它的觸摸,當它錯過我想system.out(「錯過」)。我無法弄清楚如何看它的感動。現在我只能看到屏幕是否觸摸,並且每次觸摸都會記錄大約10次觸摸,因爲它呈現得如此之快。如何在libgdx中查看紋理是否被觸摸?

public void render(float delta) { 
    Gdx.gl.glClearColor(0,1,0,1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 



    camera.update(); 
    game.batch.setProjectionMatrix(camera.combined); 
    game.batch.begin(); 
    if(Gdx.input.isTouched()) 
    { 
     int randomX2 = (int)MathUtils.random(100,500); 
    int randomY2 = (int)MathUtils.random(100,500); 
     game.batch.draw(boxImage, randomX2, randomY2); 

    } 

    game.batch.end(); 

} 

回答

7

如果randomX2randomY2是你的座標的紋理,你可以用這個代碼檢查:

Rectangle bounds = new Rectangle(randomX2, randomY2, boxImage.getWidth(), boxImage.getHeight()); 
Vector3 tmp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); 
camera.unproject(tmp); 

if (bounds.contains(tmp.x, tmp.y)) { 
    System.out.println("Is touched"); 
}