Reputation: 61
I have a screen that is opened from a main menu. the screen is shown correctly but the event touchdown doesn't work, If I use only a Game class it work but in a new screen the code isn't executed....Sorry if this is a stupid question but I am really newbie in libgdx....this is the code....
public class GameScreen implements ApplicationListener, InputProcessor,Screen {
private BitmapFont font;
SpriteBatch batch;
private Stage stage;
private Sound mp3Sound;
public GameScreen() {
super();
System.out.println("costruttore");
Gdx.input.setCatchBackKey(true);
batch = new SpriteBatch();
stage = new Stage();
Gdx.input.setInputProcessor(this);
render();
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("touchDown");
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
System.out.println("touchup");
return false;
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
public boolean scrolled(int amount) {
return false;
}
private boolean checkcollision(Rectangle attore1, Rectangle attore2) {
System.out.println("checkcollisions........");
mp3Sound.play();
return flagcollision;
}
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.BACK) {
Gdx.app.exit(); }
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public void show() {
}
@Override
public void render(float delta) {
System.out.println("render gioco");
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
stage.act(Gdx.graphics.getDeltaTime());
stage.getBatch().begin();
stage.getBatch().draw(img, 0, 0, Gdx.graphics.getWidth() / 10, Gdx.graphics.getHeight());
stage.getBatch().end();
stage.draw();
batch.end();
}
@Override
public void create() {
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
Upvotes: 1
Views: 189
Reputation: 3815
First off, you can't just call render whenever you like, you have to let the UI thread handle that. Second off, I presume this is the "second" screen you talk about? You can't have multiple screens be the applicationListener.
Basically what i would do is something like (pseudo code):
public class GameClass implements Game/ApplicationListener
public class ScreenOne implements Screen, InputProcessor
public class ScreenTwo implements Screen, InputProcessor
Have the GameClass setScreen(screenOne) in onCreate() have ScreenOne call setScreen(screenTwo) in an OnClick/touchUp function put Gdx.input.setInputProcessor(this); in show() of BOTH screens
Upvotes: 1
Reputation: 1504
Change your touch down method return value
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("touchDown");
return true;
}
Upvotes: 0