如何将Action添加到libgdx中的纹理打包器? [英] how can I add Action to a texture packer in libgdx?

查看:61
本文介绍了如何将Action添加到libgdx中的纹理打包器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在每个图像(playIcon,pauseIcon)上创建eventListner,但使用touchUp和touchDown不能正常工作

I'm trying to create eventListner on each image (playIcon,pauseIcon) but its not working using touchUp and 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()".当我单击按钮时,游戏会暂停,但是只要我松开手指,就不会暂停,游戏就会运行.

What happens now is that the button listener behaves as 'istouched()' rather than 'justtouched()'. When I click the button, the game is paused but whenever I remove my finger, there's no pause, the game runs.

推荐答案

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

If I understand correctly, you want this button to toggle between Play and Pause, calling either pause() or resume() based on current state.

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

The Button class already has a built-in internal InputListener, so all you need is a ChangeListener to react to button presses:

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

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

Make sure you mark it final when you first declare it, so the listener can reference it:

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

这篇关于如何将Action添加到libgdx中的纹理打包器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆