2016-12-04 37 views
0

目前我正在使用Libgdx在Java中開發交易卡遊戲。每個玩家都有許多具有不同能力和屬性的牌。每個卡片類都擴展了Actor類,但是我想知道是否有方法可以將文本繪製到這些Actor上,這些卡片可以移動。Libgdx Actor文本

例如,每張卡片都有一定數量的生命值,這取決於卡片所受的傷害。我想知道將這些生命值放入卡片的最佳方式是什麼,這樣當卡片移動時,生命值將隨之移動。偶爾,牌會疊加或重疊,在這些情況下,我顯然希望底部的牌的文字被頂部的牌隱藏起來,而不是全部重疊。

任何指導將不勝感激。

回答

0

感謝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)); 
} 

如果具體到你的使用情況等演員當然定位...任何問題,只是問:)