【问题标题】:How to create an ImageButton with multiple Sprite/Texture layers in LibGDX?如何在 LibGDX 中创建具有多个 Sprite/纹理层的 ImageButton?
【发布时间】:2017-09-30 07:56:15
【问题描述】:

我正在设计一款需要生成大量按钮的游戏,这些按钮具有不同的背景颜色和轮廓组合。我已经有一个 Sprite 作为背景和一个 Sprite 作为轮廓,并为每个应用着色。

我已经尝试在 SpriteBatch 上加入两者,但没有成功将其转换为 ImageButton 支持的结构。

提前致谢。

【问题讨论】:

    标签: libgdx textures sprite imagebutton spritebatch


    【解决方案1】:

    您可以通过扩展 Actor 类并实现自己的绘图方法来制作自己的 ImageButton 版本。下面的示例未经测试,但应该让您了解如何制作自己的自定义按钮:

    private class LayeredButton extends Actor{
        private int width = 100;
        private int height = 75;
        private Texture backGround;
        private Texture foreGround;
        private Texture outline;
    
        public LayeredButton(Texture bg, Texture fg, Texture ol){
            setBounds(this.getX(),this.getY(),this.width,this.height);
            backGround = bg;
            foreGround = fg;
            outline = ol;
            addListener(new InputListener(){
                public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                    // do something on click here
                    return true;
                }
            });
        }
        @Override
        public void draw(Batch batch, float alpha){
            // draw from back to front
            batch.draw(backGround,this.getX(),this.getY());
            batch.draw(foreGround,this.getX(),this.getY());
            batch.draw(outline,this.getX(),this.getY());
        }
    
        @Override
        public void act(float delta){
            // do stuff to button
        }
    }
    

    【讨论】:

    • 这实际上是一个非常简单的想法,覆盖了draw 方法。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2013-12-04
    • 2014-08-08
    • 1970-01-01
    • 2012-07-24
    • 2016-01-26
    相关资源
    最近更新 更多