【发布时间】:2017-09-30 07:56:15
【问题描述】:
我正在设计一款需要生成大量按钮的游戏,这些按钮具有不同的背景颜色和轮廓组合。我已经有一个 Sprite 作为背景和一个 Sprite 作为轮廓,并为每个应用着色。
我已经尝试在 SpriteBatch 上加入两者,但没有成功将其转换为 ImageButton 支持的结构。
提前致谢。
【问题讨论】:
标签: libgdx textures sprite imagebutton spritebatch
我正在设计一款需要生成大量按钮的游戏,这些按钮具有不同的背景颜色和轮廓组合。我已经有一个 Sprite 作为背景和一个 Sprite 作为轮廓,并为每个应用着色。
我已经尝试在 SpriteBatch 上加入两者,但没有成功将其转换为 ImageButton 支持的结构。
提前致谢。
【问题讨论】:
标签: libgdx textures sprite imagebutton spritebatch
您可以通过扩展 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 方法。非常感谢。