有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何向libgdx中的纹理打包器添加操作?

我试图在每个图像上创建eventListner(playIcon、PauseCon),但使用触地和触地都不起作用

这是我的密码:

TextureAtlas buttonsPlayPause = new TextureAtlas("uiii/uiii.pack");
skin.addRegions(buttonsPlayPause);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("pauseIcon");
textButtonStyle.checked = skin.getDrawable("playIcon");
TextButton button = new TextButton("", textButtonStyle);
button.setY(pauseY);
button.setX(Gdx.graphics.getWidth()/6);
button.setSize(150,150);
stage.addActor(button);

//pause/playAction
button.addListener(new ClickListener() {
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {resume();
              }
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
         pause();
            return true;
        }
    });

现在发生的是按钮侦听器的行为是“istouched()”而不是“justtouched()”。当我点击按钮时,游戏暂停,但每当我移开手指,没有暂停,游戏就会运行


共 (1) 个答案

  1. # 1 楼答案

    如果我理解正确,您希望此按钮在播放和暂停之间切换,根据当前状态调用pause()resume()

    Button类已经有一个内置的内部InputListener,因此您只需要一个ChangeListener来对按钮按下做出反应:

    //pause/playAction
    button.addListener(new ChangeListener() {
        @Override
        public void changed (ChangeEvent event, Actor actor) {
            if (button.isChecked())
                resume();
            else
                play();
        }
    });
    

    确保在首次声明时将其标记为final,以便侦听器可以引用它:

    //...
    final TextButton button = new TextButton("", textButtonStyle);
    //...