Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使此libGDX阶段/按钮接收触摸?_Java_Android Layout_Libgdx - Fatal编程技术网

Java 如何使此libGDX阶段/按钮接收触摸?

Java 如何使此libGDX阶段/按钮接收触摸?,java,android-layout,libgdx,Java,Android Layout,Libgdx,我能够获得要渲染的图像和按钮小部件,但是,我尚未成功获得buttonPlay以接收输入。这一假设是由于按钮的向下图像在触摸时未呈现,并且onPlayClicked()也未执行 我相信我正确地将Stage设置为输入处理器 我已经在S.O.上穷尽了寻找解决方案,但毫无结果 有问题的按钮是ScreenMenuMain.java的buildControlsLayer()中的buttonPlay 编辑:屏幕流程:我想从ScreenMenuMain.java切换到另一个名为ScreenGame.java的屏

我能够获得要渲染的图像和按钮小部件,但是,我尚未成功获得buttonPlay以接收输入。这一假设是由于按钮的向下图像在触摸时未呈现,并且onPlayClicked()也未执行

我相信我正确地将Stage设置为输入处理器

我已经在S.O.上穷尽了寻找解决方案,但毫无结果

有问题的按钮是ScreenMenuMain.java的buildControlsLayer()中的buttonPlay

编辑:屏幕流程:我想从ScreenMenuMain.java切换到另一个名为ScreenGame.java的屏幕

我正试图通过DirectedGame.java实现这一点,它指导屏幕切换。它还添加了过渡效果,使前一阶段的输入处理器无效,并将输入处理器从新屏幕设置为该阶段

ScreenMenuMain.java

    public class ScreenMenuMain extends AbstractGameScreen {

    private Stage           stage;
    private Skin            skinMenuMain;

    //menu
    private Image           menuLogo;
    private Button          buttonPlay;

    public ScreenMenuMain (DirectedGame game) {
        super(game);
    }

    @Override
    public void render (float deltaTime) {
        Gdx.gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(deltaTime);
        stage.draw();
    }

    @Override
    public void resize (int width, int height) {
        stage.setViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT, false);
    }

    @Override
    public void show () {
        stage = new Stage();
        rebuildStage();
    }

    @Override
    public void hide () {
        stage.dispose();
        skinMenuMain.dispose();
    }

    private void rebuildStage () {
        skinMenuMain = new Skin(Gdx.files.internal(Constants.SKIN_MAINMENU), new TextureAtlas(Constants.TEXTURE_ATLAS_MAINMENU));

        // build all layers
        Table layerLogo = buildLogoLayer();
        Table layerControls = buildControlsLayer();

        // assemble stage for menu screen
        stage.clear();
        Stack stack = new Stack();
        stage.addActor(stack);
        stack.setSize(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT);
        stack.add(layerLogo);
        stack.add(layerControls);
    }

    private Table buildLogoLayer () {
        Table layer = new Table();
        layer.top();
        // + Logo
        menuLogo = new Image(skinMenuMain, "menulogo");
        layer.add(menuLogo);
        layer.row().expandY();
        return layer; 
    }

    private Table buildControlsLayer () {
        Table layer = new Table();
        layer.left().bottom();
        //add play button
        buttonPlay = new Button(skinMenuMain, "play"); 
        layer.add(buttonPlay);
        buttonPlay.addListener(new ChangeListener() {
            @Override
            public void changed (ChangeEvent event, Actor actor) {
                System.out.println("touch down");
                onPlayClicked();
            }
        });         

        return layer;
    }

    private void onPlayClicked () {
        ScreenTransition transition = ScreenTransitionFade.init(0.75f);
        game.setScreen(new ScreenGame(game), transition);
    }

    @Override
    public InputProcessor getInputProcessor () {
        return stage;
    }
}
DirectedGame.java[用于设置输入处理器和切换屏幕]

    public abstract class DirectedGame implements ApplicationListener {

    private boolean init;
    private AbstractGameScreen currScreen;
    private AbstractGameScreen nextScreen;
    private FrameBuffer currFbo;
    private FrameBuffer nextFbo;
    private SpriteBatch batch;
    private float t;
    private ScreenTransition screenTransition;

    public void setScreen (AbstractGameScreen screen) {
        setScreen(screen, null);
    }

    public void setScreen (AbstractGameScreen screen, ScreenTransition screenTransition) {
        int w = Gdx.graphics.getWidth();
        int h = Gdx.graphics.getHeight();
        if (!init) {
            currFbo = new FrameBuffer(Format.RGB888, w, h, false);
            nextFbo = new FrameBuffer(Format.RGB888, w, h, false);
            batch = new SpriteBatch();
            init = true;
        }
        // start new transition
        nextScreen = screen;
        nextScreen.show(); // activate next screen
        nextScreen.resize(w, h);
        nextScreen.render(0); // let next screen update() once
        if (currScreen != null) currScreen.pause();
        nextScreen.pause();
        Gdx.input.setInputProcessor(null); // disable input
        this.screenTransition = screenTransition;
        t = 0;
    }

    @Override
    public void render () {
        // get delta time and ensure an upper limit of one 60th second
        float deltaTime = Math.min(Gdx.graphics.getDeltaTime(), 1.0f / 60.0f);
        if (nextScreen == null) {
            // no ongoing transition
            if (currScreen != null) currScreen.render(deltaTime);
        } else {
            // ongoing transition
            float duration = 0;
            if (screenTransition != null) duration = screenTransition.getDuration();
            t = Math.min(t + deltaTime, duration);
            if (screenTransition == null || t >= duration) {
                // no transition effect set or transition has just finished
                if (currScreen != null) currScreen.hide();
                nextScreen.resume();
                // enable input for next screen
                Gdx.input.setInputProcessor(nextScreen.getInputProcessor());
                // switch screens
                currScreen = nextScreen;
                nextScreen = null;
                screenTransition = null;
            } else {
                // render screens to FBOs
                currFbo.begin();
                if (currScreen != null) currScreen.render(deltaTime);
                currFbo.end();
                nextFbo.begin();
                nextScreen.render(deltaTime);
                nextFbo.end();
                // render transition effect to screen
                float alpha = t / duration;
                screenTransition.render(batch, currFbo.getColorBufferTexture(), nextFbo.getColorBufferTexture(), alpha);
            }
        }
    }

    @Override
    public void resize (int width, int height) {
        if (currScreen != null) currScreen.resize(width, height);
        if (nextScreen != null) nextScreen.resize(width, height);
    }

    @Override
    public void pause () {
        if (currScreen != null) currScreen.pause();
    }

    @Override
    public void resume () {
        if (currScreen != null) currScreen.resume();
    }

    @Override
    public void dispose () {
        if (currScreen != null) currScreen.hide();
        if (nextScreen != null) nextScreen.hide();
        if (init) {
            currFbo.dispose();
            currScreen = null;
            nextFbo.dispose();
            nextScreen = null;
            batch.dispose();
            init = false;
        }
    }
}

您必须像这样将InputListener添加到按钮

buttonPlay.addListener(new InputListener(){

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("touch down");
            onPlayClicked();
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            // Do some stuff on touchUp
            super.touchUp(event, x, y, pointer, button);
        }

    });

另请查看ClickListeners: