Libgdx将超过2个Sprite数组组合为1个Sprite数组

时间:2018-08-14 17:16:45

标签: java arrays libgdx

我想知道他们是否可以将两个以上的精灵数组组合成一个精灵数组。例如,我想将这4组卡片套装组合成1组。 (来自com.badlogic.gdx.utils.Array的数组)

public class Diamonds extends CardTypes {
public Array<Sprite> diamonds = new Array<Sprite>();

public void cards(){
    diamonds.add(ace = new Sprite(new Texture("diamonds/AD.png")));
    diamonds.add(two = new Sprite(new Texture("diamonds/2D.png")));
    diamonds.add(three = new Sprite(new Texture("diamonds/3D.png")));}}

public class Spades extends CardTypes{
        public Array<Sprite> spades = new Array<Sprite>();

    public void cards(){
        spades.add(ace = new Sprite(new Texture("spades/AS.png")));
        spades.add(two = new Sprite(new Texture("spades/2S.png")));
        spades.add(three = new Sprite(new Texture("spades/3S.png")));}}

public class Clubs extends CardTypes {
public Array<Sprite> clubs = new Array<Sprite>();

public void cards(){
    clubs.add(ace = new Sprite(new Texture("clubs/AC.png")));
    clubs.add(two = new Sprite(new Texture("clubs/2C.png")));
    clubs.add(three = new Sprite(new Texture("clubs/3C.png")));}}

public class Hearts extends CardTypes {
public Array<Sprite> hearts = new Array<Sprite>();

public void cards(){
    hearts.add(ace = new Sprite(new Texture("hearts/AH.png")));
    hearts.add(two = new Sprite(new Texture("hearts/2H.png")));
    hearts.add(three = new Sprite(new Texture("hearts/3H.png")));}}

这些子画面数组中的每一个都可以容纳3张卡,我尝试创建一个不同的子画面数组,并使用com.badlogic.gdx.utils.Array中的addAll()方法,但是它似乎没有用。

1 个答案:

答案 0 :(得分:1)

创建对象并保留所有CardTypes的引用

Diamonds diamond= new Diamond();
diamond.cards();                 // add sprites into diamonds array

Spades spade = new Spades();
spade.cards();

Clubs club=new Club();
club.cards();

Hearts heart=new Hearts();
heart.cards();

现在将所有卡合并到一个Array中:

Array allCard = new Array<Sprite>();
allCard.addAll(diamond.diamonds);
allCard.addAll(spade.spades);
allCard.addAll(club.clubs);
allCard.addAll(heart.hearts);