新屏幕未收到触地事件

时间:2015-12-02 09:54:57

标签: android input libgdx screen

我有一个从主菜单打开的屏幕。屏幕显示正确,但事件触地不起作用,如果我只使用一个游戏类它工作,但在新的屏幕上代码不执行....对不起,如果这是一个愚蠢的问题,但我真的是新手在libgdx ....这是代码....

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() {

    }



}

2 个答案:

答案 0 :(得分:1)

首先,你不能随时调用渲染,你必须让UI线程处理它。第二关,我认为这是你谈到的“第二”屏幕?您不能将多个屏幕作为applicationListener。

基本上我会做的就是(伪代码):

public class GameClass implements Game/ApplicationListener
public class ScreenOne implements Screen, InputProcessor
public class ScreenTwo implements Screen, InputProcessor

在onCreate()中使用GameClass setScreen(screenOne) 让ScreenOne在OnClick / touchUp函数中调用setScreen(screenTwo) 把Gdx.input.setInputProcessor(this);在两个屏幕的show()中

答案 1 :(得分:0)

更改您的降落方法返回值

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
         System.out.println("touchDown");   

    return true;
}