感謝libGDX提供的繼承,有一個簡單的解決方案。
由於Group
是一個演員,你可以輕鬆地做...(僞代碼)
public class Card extends Group {
final private static TextureAtlas ATLAS;
static {
ATLAS = AssetManager.get("game.pack", TextureAtlas.class);
}
public enum Type {
type1, type2, type3
}
private Type type;
private Image card;
private Label text;
private int hitPoints;
public Card(Type type) {
this.type = type;
card = new Image(ATLAS.get(type));
text = new Label("HP: " + hitPoints);
addActor(card);
addActor(text);
}
}
這裏假設你有一個與你的紋理圖集的標識符相關聯卡類型枚舉;如果你不使用紋理地圖集,我強烈建議你這樣做。
在你的遊戲邏輯類中,我假設它將是Screen
類的擴展,你可以簡單地;
private Card card;
@override
public void show() {
card = new Card(Card.Type.type1);
stage.addActor(card);
// This is an example - as the card and text reside within a group they can be moved as a sole entity relative to their parent
card.addAction(moveBy(100, 100, 2, Interpolation.Linear));
}
如果具體到你的使用情況等演員當然定位...任何問題,只是問:)