单击时更改按钮比例

时间:2014-12-25 04:38:21

标签: libgdx

我希望游戏中的所有按钮能够缩小当用户触碰按钮并返回正常一旦触摸起来就会缩放。应该是最好的方式。就像COCOS-2d中按钮的功能一样。 我不想使用超小图片来关闭按钮,如果有其他选择而不使用额外的图像。?

第二个是在点击上产生一些声音,我不想在每个按钮的输入监听器中这样做,因为所有按钮的声音都相同。我该如何以最佳方式实现这一点。

提前致谢。

//我只是实现我的自定义类,还有其他好方法可以实现。

public class ImprovedButton extends Button {    
    boolean isTouchDown=false;// is touch begin
    float originalWidth,originalHeight;     //actual size

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if( isPressed()) {
            if (!isTouchDown) {
                //play sound if any
                isTouchDown=true;
                originalHeight=getHeight();
                originalWidth=getWidth();
                setWidth(originalWidth*.95f);
                setHeight(originalHeight*.95f);
            }
        }
        else { //pressing is stopper if any
            if (isTouchDown) { 
                isTouchDown=false;
                setWidth(originalWidth);
                setHeight(originalHeight);
            }           
        }
    }    
}

此代码的一个问题是,缩放左下角的枢轴。如何将其设置为中心。

2 个答案:

答案 0 :(得分:1)

我在游戏中做同样的事情。

像按钮的自定义类一样。

如果需要,可以在更新方法中增加比例,因此按钮将平滑缩放。 看这里,我给你写了一个例子

public class CoolButton {
    private Sprite sprite; // the sprite that we are going to use to draw our button
    private float scale; // the scale of the button, 1 = full scale, 0.5 half, 2 = twice bigger, etc

    public CoolButton(Texture texture, float x, float y, float width, float height) {
        sprite = new Sprite(texture);
        sprite.setPosition(x, y);
        sprite.setSize(width, height);
        sprite.setOriginCenter();
        scale = 1;
    }

    public void draw(SpriteBatch batch) {
        sprite.setScale(scale); // setting the scale and drawing the button
        sprite.draw(batch);
    }

    // this method should be called when the user touches the screen (don't forget to unproject the coords)
    public boolean touchDown(float x, float y) {
        if (sprite.getBoundingRectangle().contains(x, y)) { // if the button is touched
            scale = 0.7f;
            return true;
        }
        return false;
    }

    // when you release the touch from the screen make the button back to its normal size
    public void touchUp() {
        scale = 1f;
    }
}

如果您有任何疑问,请在此处发表评论。

答案 1 :(得分:0)

这是一个自定义类 ImprovedButton 从Button类扩展,具有更改 SCALE 按钮的附加功能,并在点击时播放 SOUND 。  还有一类 ImprovedButtonStyle 具有规模和声音的参数。

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

public class ImprovedButton extends Button {

    boolean isPressedOnce=false;//is press started or touchDown
    float originalWidth,originalHeight;
    private ImprovedButtonStyle istyle;

    public ImprovedButton() {
        super();
        this.istyle=new ImprovedButtonStyle();
    }    
    public ImprovedButton(Actor child, ImprovedButtonStyle style) {
        super(child, style);
        this.istyle=style;
    }    
    public ImprovedButton(Actor child, Skin skin, String styleName) {
        this(child,skin.get(styleName, ImprovedButtonStyle.class));         
    }    
    public ImprovedButton(Actor child, Skin skin) {
        this(child,skin.get(ImprovedButtonStyle.class));
    }    
    public ImprovedButton(ImprovedButtonStyle style) {
        super(style);
        this.istyle=style;
    }    
    public ImprovedButton(Drawable up, Drawable down, Drawable checked) {
        super(up, down, checked);
        istyle=new ImprovedButtonStyle();
    }    
    public ImprovedButton(Drawable up, Drawable down) {
        this(up, down,null);
    }    
    public ImprovedButton(Drawable up) {
        this(up,null,null);
    }    
    public ImprovedButton(Skin skin, String styleName) {
        super(skin, styleName);
        this.istyle=skin.get(ImprovedButtonStyle.class);    
    }    
    public ImprovedButton(Skin skin) {
        super(skin);
        setStyle(skin.get(ButtonStyle.class));    
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if( isPressed()) {
            if (!isPressedOnce) {                   
                if (istyle.sound!=null) {
                  istyle.sound.play();
                }
                isPressedOnce=true;
                originalHeight=getHeight();
                originalWidth=getWidth();
                setWidth(originalWidth-istyle.changeInWidth);
                setHeight(originalHeight-istyle.changeInHeight);
            }
        }
        else {
            if (isPressedOnce) {
                isPressedOnce=false;
                setWidth(originalWidth);
                setHeight(originalHeight);
            }           
        }
    }

    @Override
    protected void drawBackground(Batch batch, float parentAlpha, float x,float y) {
         int offsetX=0;
         int offsetY=0;
         if(isPressed()){                
            offsetX=istyle.changeInWidth/2;
            offsetY=istyle.changeInHeight/2;
          }   
         super.drawBackground(batch, parentAlpha, x+offsetX, y+offsetY);
    }

    public static class ImprovedButtonStyle extends ButtonStyle{
        public int changeInWidth=2,changeInHeight=2;
        public Sound sound=null;
        public ImprovedButtonStyle() {
            super();
        }

    public ImprovedButtonStyle(ButtonStyle style,int changeInWidth,int changeInHeight,Sound sound) {
            super(style);
            this.changeInHeight=changeInHeight;
            this.changeInWidth=changeInWidth;
            this.sound=sound;
        }
    public ImprovedButtonStyle(Drawable up, Drawable down, Drawable checked) {
            super(up, down, checked);
        }
    public ImprovedButtonStyle(ImprovedButtonStyle style) {
            super(style);
            this.changeInHeight=style.changeInHeight;
            this.changeInWidth=style.changeInWidth;
            this.sound=style.sound;    
        }
    }
}