Answer the question
In order to leave comments, you need to log in
Handling button coordinates?
I have a certain MenuState in which there are several buttons, I want the coordinates in the handleTouch method to be processed and the screen with the game to start if the click happened at the given coordinates, but the code does not work and clicking anywhere on the screen launches the screen with the game. What am I doing wrong?
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.MyGdxGame;
public class MenuState extends State {
private Texture backgroundMenu;
private Texture playBtn;
private Texture settingsBtn;
private Texture exitBtn;
OrthographicCamera camera;
Vector3 position = new Vector3();
public MenuState(GameStateManager gsm) {
super(gsm);
float height= Gdx.graphics.getHeight();
float width = Gdx.graphics.getWidth();
camera = new OrthographicCamera(width,height);
camera.setToOrtho(false);
backgroundMenu = new Texture("backgroundmenu.png");
playBtn = new Texture("playbtn.png");
settingsBtn = new Texture("settingsbtn.png");
exitBtn = new Texture("exitbtn.png");
}
@Override
protected void handleInput() {
}
@Override
public void handleTouch() {
if(Gdx.input.justTouched()) {
position.set(Gdx.input.getX(0), Gdx.input.getY(50),0);
camera.unproject(position);
gsm.set(new PlayState(gsm));
}
}
@Override
public void update(float dt) {
handleTouch();
}
@Override
public void render(SpriteBatch sb) {
sb.begin();
sb.draw(backgroundMenu, 0, 0, MyGdxGame.WIDTH, MyGdxGame.HEIGHT);
sb.draw(playBtn, 0, 50,64, 64);
sb.draw(settingsBtn, 200, 50,64, 64);
sb.draw(exitBtn, 400, 50,64, 64);
sb.setProjectionMatrix(camera.combined);
sb.end();
}
@Override
public void dispose() {
backgroundMenu.dispose();
playBtn.dispose();
settingsBtn.dispose();
exitBtn.dispose();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question