如何在LibGDX中用多个Sprite/Texture图层创建一个ImageButton?

问题描述:

我正在设计一款游戏,需要生成大量的按钮,以及背景颜色和轮廓的不同组合。我已经有一个Sprite的背景和一个Sprite的轮廓,并为每个应用色调。如何在LibGDX中用多个Sprite/Texture图层创建一个ImageButton?

我已经尝试加入两个SpriteBatch,但没有运气将其转换为ImageButton支持的结构。

在此先感谢。

您可以通过扩展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 
    } 
} 
+0

这实际上是一个非常简单的想法,覆盖'draw'方法。非常感谢你。 –