i want to touch a specific texture on my screen to start my game but my problem is when i touch anywhere it start the game
public void handleInput() {
if(Gdx.input.isTouched()){
//gsm is a GameStateManger
gsm.set(new PlayState(gsm));
dispose();
Rectangle bounds = new Rectangle((Gdx.graphics.getWidth() / 2)-(playButton.getWidth()/2)+55,Pharaonic.hight/12,480,193); //corners of the image/texture
Vector3 Point;
Point=new Vector3(Gdx.input.getX(), Gdx.input.getY(),0);
Point.set(Gdx.input.getX(), Gdx.input.getY(),0);
if(bounds.contains(Point.x, Point.y)){
gsm.set(new PlayState(gsm));
dispose();
}}
}
and here is the update method
@Override
public void update(float dt) {
handleInput();
}
I really appreciate any help solving this one.
Use Sprite
instead of Texture
, Sprite holds the geometry, color, and texture information for drawing 2D sprites using batch.
Initialize playSprite in show()
or create()
method
playSprite=new Sprite(playButton);
playSprite.setSize(200,100); // set size
playSprite.setPosition(Gdx.graphics.getWidth()/2-playSprite.getWidth()/2,Gdx.graphics.getHeight()/2-playSprite.getHeight()/2); //set position
Render your Sprite
playSprite.draw(batch);
Bound your touch with sprite bounding rectangle.
@Override
public void handleInput() {
if (Gdx.input.isTouched()) {
if (playSprite.getBoundingRectangle().contains(Gdx.input.getX(), Gdx.input.getY())) {
gsm.set(new PlayState(gsm));
dispose();
}
}
}
EDIT
@Override
public void handleInput() {
if (Gdx.input.isTouched()) {
if (playSprite.getBoundingRectangle().contains(Gdx.input.getX(), Gdx.graphics.getHeight()-Gdx.input.getY())) {
gsm.set(new PlayState(gsm));
dispose();
}
}
}