LibGDX在单击鼠标按钮时更新标签

时间:2014-07-01 16:08:42

标签: java libgdx label

我有一个从0到100设置的浮点值,我希望注册长按并逐步更新Label,这样每隔0.25秒就会更新标签的文本。目前我正在使用自定义标签,该标签具有更新显示文本的方法 我试过像这样使用touchDown:

while (Gdx.input.isButtonPressed(Buttons.LEFT)) {
    double currentValue = Double.parseDouble(Button.getText()
            .toString());
    if ((int) currentValue != 0)
        currentValue--;
    Button.updateText(Double.toString(currentValue));
}

但这似乎不起作用,如何在点击按钮时更新标签?

编辑:ClickListener的大部分代码:

        @Override
        public void enter(InputEvent event, float x, float y, int pointer,
                Actor fromActor) {
            super.enter(event, x, y, pointer, fromActor);
            TextButtonStyle style = ButtonLower.getStyle();
            style.font = red;
            ButtonLower.setStyle(style);
        }

        @Override
        public void exit(InputEvent event, float x, float y, int pointer,
                Actor toActor) {
            super.exit(event, x, y, pointer, toActor);
            TextButtonStyle style = ButtonLower.getStyle();
            style.font = white;
            ButtonLower.setStyle(style);
        }

        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            System.out.println(event.toString());
            double currentValue = Double.parseDouble(Button.getText()
                    .toString());
            if ((int) currentValue != 0)
                currentValue--;
            Button.updateText(Double.toString(currentValue));
        }

        @Override
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
                double currentValue = Double.parseDouble(Button.getText()
                        .toString());
                if ((int) currentValue != 0)
                    currentValue--;
                Button.updateText(Double.toString(currentValue));
            }
            return super.touchDown(event, x, y, pointer, button);
        }

1 个答案:

答案 0 :(得分:1)

在不知道哪个类实现了您的InputListener的情况下,有点难以告诉您如何解决问题。但是,在一般意义上,您可能想要做的是在touchDown方法上将布尔标志设置为true,在touchUp方法上设置为false,然后在某些方法中调用每个框架(actdraw等,具体取决于您的实施情况),您需要更新文字。

代码看起来像

private boolean flag = false;

@Override
public boolean touchDown(InputEvent event, float x, float y,
        int pointer, int button) {
    if (button == Buttons.LEFT) {
        flag = true;    
    }
    //other stuff
    return true; //or whatever you want
}

@Override
public boolean touchUp(InputEvent event, float x, float y,
        int pointer, int button) {
    if (button == Buttons.LEFT) {
        flag = false;    
    }
    //other stuff
    return false; //or whatever you want
}

@Override
public void act(float delta) {
    if (flag) {
        double currentValue = Double.parseDouble(Button.getText()
                    .toString());
            if ((int) currentValue != 0)
                currentValue--;
            Button.updateText(Double.toString(currentValue));
    }
    //other stuff
}     

取决于您的目标受众,您可能还必须以某种方式处理指针ID(如果用户用两个手指点击标签,然后释放一个但不释放另一个,则此代码示例将无效)