【问题标题】:how can I add Action to a texture packer in libgdx?如何将动作添加到 libgdx 中的纹理打包器?
【发布时间】:2017-03-28 22:31:46
【问题描述】:

我正在尝试在每个图像(playIcon,pauseIcon)上创建 eventListner,但它无法使用 touchUp 和 touchDown

这是我的代码:

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()”。当我点击按钮时,游戏会暂停,但每当我移开手指时,游戏不会暂停。

【问题讨论】:

  • 你有没有通过这个 Gdx.input.setInputProcessor(stage); 将 stage 设置为 InputProcessor;
  • 从您的评论中仍然不清楚您想要发生什么,以及它目前的行为有什么问题。
  • @Tenfour04。现在发生的是按钮侦听器的行为类似于“istouched()”而不是“justtouched()”。当我点击按钮时,游戏暂停,但每当我移开手指时,没有暂停,游戏运行
  • @AbhishekAryan 请阅读我的编辑
  • 这是预期的,因为您在 touchUp 方法中调用了 resume()

标签: java android libgdx


【解决方案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);
//...

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 2015-03-26
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多