简体   繁体   English

libGDX更改按钮的图像

[英]libGDX changing button's image

By following Kilbolt's Zombie Bird tutorial I created in my project a class to represent all buttons: 通过遵循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;
}
}

Whenever I want to create a button I do it in my InputProcessor class by creating a SimpleButton object and after filling its parameters I add it to List list. 每当我想创建一个按钮时,我都会在InputProcessor类中通过创建一个SimpleButton对象来完成此操作,并在填充其参数之后将其添加到“列表”列表中。 My current problem is that I'm trying to create a sound button, which I did create and it work fine, but what I'm struggling with is making the button change TextureRegion between ON and OFF states. 我当前的问题是我试图创建一个声音按钮,该按钮确实创建了并且可以正常工作,但是我正在努力使按钮在ON和OFF状态之间更改TextureRegion。 This is an example of how I create a button(actually I created the sound button) and put it in the list: 这是如何创建按钮(实际上是创建声音按钮)并将其放入列表的示例:

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

I tried using the same condition that when sound ON is on it will put inside the parameters "AssetLoader.soundOnNotClicked" and when it's OFF "AssetLoader.soundOFFNotClicked", but it just show the first TextureRegion in the condition. 我尝试使用相同的条件,即当声音打开时,它将放入参数“ AssetLoader.soundOnNotClicked”,而当其关闭时,将其放入“ AssetLoader.soundOFFNotClicked”,但它只显示条件中的第一个TextureRegion。 Can someone help me figure out how I can change TextureRigions when sound is ON and OFF? 有人可以帮我弄清楚当声音打开和关闭时如何更改TextureRigions吗?

EDIT WITH CHANGES: First created a second constractor: 更改内容:首先创建了第二个承包商:

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);

}

Then changed a bit the isClicked method: 然后更改了isClicked方法:

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

Then changed the draw method: 然后更改了draw方法:

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);
        }
    }
}

Lastly I changed the creation of the button by adding the textureRegion of the OFF sound: 最后,我通过添加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. Make another boolean called isOn just like isPressed 像isPressed一样创建另一个名为isOn的布尔值
  2. Add another TextureRegion called buttonOn just like buttonDown 添加另一个名为buttonOn的TextureRegion,就像buttonDown一样
  3. Add TextureRegion buttonDown to your constructor and set it there like you do with buttonDown 将TextureRegion buttonDown添加到构造函数中,并像在buttonDown一样在此处进行设置
  4. Flip the boolean whenever the button is clicked in isClicked(). 每当在isClicked()中单击按钮时,翻转布尔值。 (isOn = !isOn) (isOn =!isOn)
  5. Add the condition in your draw method to draw the buttonOn texture if isOn is true. 如果isOn为true,则在draw方法中添加条件以绘制buttonOn纹理。

That should pretty much do it. 那应该差不多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM