libGDX更改按钮的图像

时间:2014-11-17 15:17:34

标签: java android libgdx

通过遵循Kilbolt的Zombie Bird教程,我在项目中创建了一个代表所有按钮的类:

public class SimpleButton {

private float x, y, width, height;

private TextureRegion buttonUp;
private TextureRegion buttonDown;

private Rectangle bounds;

private boolean isPressed = false;

public SimpleButton(float x, float y, float width, float height,
        TextureRegion buttonUp, TextureRegion buttonDown) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.buttonUp = buttonUp;
    this.buttonDown = buttonDown;

    bounds = new Rectangle(x, y, width, height);

}

public boolean isClicked(int screenX, int screenY) {
    return bounds.contains(screenX, screenY);
}

public void draw(SpriteBatch batcher) {
    if (isPressed) {
        batcher.draw(buttonDown, x, y, width, height);
    } else {
        batcher.draw(buttonUp, x, y, width, height);
    }
}

public boolean isTouchDown(int screenX, int screenY) {

    if (bounds.contains(screenX, screenY)) {
        isPressed = true;
        return true;
    }

    return false;
}

public boolean isTouchUp(int screenX, int screenY) {

    // It only counts as a touchUp if the button is in a pressed state.
    if (bounds.contains(screenX, screenY) && isPressed) {
        isPressed = false;
        return true;
    }

    // Whenever a finger is released, we will cancel any presses.
    isPressed = false;
    return false;
}
}

每当我想创建一个按钮时,我通过创建一个SimpleButton对象在我的InputProcessor类中完成它,并在填充其参数后将其添加到List列表中。 我目前的问题是我试图创建一个声音按钮,我创建它并且它工作正常,但我正在努力的是让按钮在开启和关闭状态之间改变TextureRegion。 这是我如何创建一个按钮(实际上是我创建了声音按钮)并将其放入列表中的示例:

    soundButton = new SimpleButton(
            136 / 2 - (AssetLoader.soundOnNotClicked.getRegionWidth() / 0.25f),
            midPointY - 102, 15, 15, AssetLoader.soundOnNotClicked,
            AssetLoader.soundClicked);
    gameButtons.add(backButton);

我尝试使用相同的条件,当声音打开时,它将放入参数" AssetLoader.soundOnNotClicked"当它关闭" AssetLoader.soundOFFNotClicked"时,它只显示条件中的第一个TextureRegion。有人可以帮我弄清楚当声音打开和关闭时我怎么能改变TextureRigions?

编辑更改: 首先创建了第二个约束器:

public SimpleButton(float x, float y, float width, float height,
        TextureRegion buttonUp, TextureRegion buttonDown, TextureRegion buttonOn) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.buttonUp = buttonUp;
    this.buttonDown = buttonDown;
    this.buttonOn = buttonOn;

    bounds = new Rectangle(x, y, width, height);

}

然后改变了一下isClicked方法:

public boolean isClicked(int screenX, int screenY) {
    if (isOn == false) {
        isOn = true;
    }else {
        isOn = false;
    }
    return bounds.contains(screenX, screenY);
}

然后改变了绘制方法:

public void draw(SpriteBatch batcher) {
    if (isPressed) {
        batcher.draw(buttonDown, x, y, width, height);
    } else {
        if (isOn == false) {
            batcher.draw(buttonUp, x, y, width, height);
        }else{
            batcher.draw(buttonOn, x, y, width, height);
        }
    }
}

最后,我通过添加OFF声音的textureRegion来改变按钮的创建:

    soundButton = new SimpleButton(
            136 / 2 - (AssetLoader.soundButtonOff.getRegionWidth() / 0.25f),
            midPointY - 102, 15, 15, AssetLoader.soundButtonOff,
            AssetLoader.soundButtonOn, AssetLoader.soundButtonOffX);
    menuButtons.add(soundButton);

1 个答案:

答案 0 :(得分:0)

  1. 创建另一个名为isOn的布尔值,就像isPressed一样
  2. 添加另一个名为buttonOn的TextureRegion,就像buttonDown
  3. 一样
  4. 将TextureRegion buttonDown添加到构造函数中并像设置buttonDown一样将其设置为
  5. 只要在isClicked()中单击按钮,就翻转布尔值。 (isOn =!isOn)
  6. 如果isOn为true,则在draw方法中添加条件以绘制buttonOn纹理。
  7. 那几乎应该这样做。