【问题标题】:Array List Variables Using LibGDX使用 LibGDX 的数组列表变量
【发布时间】:2016-01-05 18:48:07
【问题描述】:

我正在开发 LibGDX 中的游戏,但我被数组列表困住了。我的计划是有一副 20 张牌。数组列表中的每个位置都有自己的卡片,其中包含统计值。

下面是我的卡片类:

public class Card {   
    private String name;
    private int force;
    private int attack;
    private int defense;
    private int cost;
    public Card(String name, int force, int attack, int defense, int cost){
        this.name = name;
        this.attack = attack;
        this.cost = cost;
        this.defense = defense;
        this.force = force;
    }    
    public int force (){
        return this.force;      
    }   
    public int attack (){
        return this.attack;      
    }  
    public int defense (){
        return this.defense; 
    }   
    public int cost (){
        return this.cost;
    }    
    public String name(){
        return this.name;
    }  
}

这是我的套牌类,我需要帮助我了解实现卡片值的代码。

public class Deck {  
    private Array<Card> cards;
    private String name;
    private int force;
    private int attack;
    private int defense;
    private int cost;  
    public Deck(){
        cards.size = 20;
    }
    
    public void createDeck(){
        
    }
    public void shuffle(){
        
    }   
    public void draw(){
        
    }   
    public void add(){       
    }   
}

简而言之,我需要帮助遍历阵列列表并将攻击、防御、成本和力量放置到每个阵列位置。任何帮助或建议将不胜感激。

【问题讨论】:

    标签: java arrays libgdx


    【解决方案1】:

    您不能像这样初始化Array 的大小:

    public Deck(){
        cards.size = 20;
    }
    

    Array 是一个接口,因此您需要一个ArrayList 的实例。 ArrayList 是一个动态数组对象,所以不需要提供数组的大小。

    您只需要将add 的元素添加到数组中。

    或者你可以使用原始数组,但是你应该小心,因为这种类型的数组不能动态改变它的大小。

    first 选项可以是这样的:

    private Array<Card> cards;
    
    ...
    
    public void createDeck(){
    
       cards = new ArrayList<Card>();
    
       cards.add(new Card("Name1", 1, 2, 3, 4));
       cards.add(new Card("Name2", 5, 6, 7, 8));
       cards.add(new Card("Name3", 9, 10, 11, 12));
    
    }
    

    原始数组的第二个选项:

    private Card cards[];
    
    ...
    
    public void createDeck(){
    
       cards = new Card[20];
    
       cards[0] = new Card("Name1", 1, 2, 3, 4);
       cards[1] = new Card("Name2", 5, 6, 7, 8);
       ...
       cards[19] = new Card("Name20", 50, 60, 70, 80);
    
    }
    

    【讨论】:

      【解决方案2】:

      如果您可以使用 Java 8,我推荐使用流 API。 例如,

      public void createDeck(Array<Card> cards, String name){
          this.cards = cards;
          this.name = name;
          this.force = cards.stream()//a stream is a functional, glorified forEach with a nice API 
              .mapToInt(Card::force) //changes the stream into ints (the card's force value)
              .sum();                //gets the total.
          . . .
      }
      

      请参阅https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.htmlhttps://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html 了解更多信息。

      如果您需要更多指导,请发表评论,我会详细说明。

      【讨论】:

        【解决方案3】:

        你有没有尝试过像这样制作一个字符串数组

        int ammountOfMethods = 7; //Arbitrary number, but whatever you'd need
        String[]  stringArray = new String[ammountOfMethods];
        stringArray[0] = "force";
        //Etc...
        

        【讨论】:

          【解决方案4】:

          套牌具有与卡牌相同的属性值(名称、力量、攻击、防御、费用)是否有特定原因?因为似乎牌组应该只容纳卡片,然后每张卡片都有自己的一组统计数据,并且每张卡片的统计数据会有所不同。

          关于创建卡片并赋予它们初始值,您必须以某种方式指定每张卡片的统计信息。您可以对这些值进行硬编码,或者如果您希望每次玩牌时卡片的统计数据都不同,您可以使用随机数生成来选择具有指定范围的值。然后你需要从这些值实例化(创建)卡片并将它们添加到你的牌组中。

          createDeck() 方法是您想要创建卡片组、创建卡片并将它们添加到卡片组的地方。

          下面是硬编码卡牌“骑士”、“巨魔”和“巫师”的统计值并将它们添加到牌组的示例:

          public void createDeck(){
             cards = new Array<Card>(Card.class);
          
             cards.add(new Card("Knight", 50, 25, 35, 200));
             cards.add(new Card("Troll", 25, 27, 29, 350));
             cards.add(new Card("Wizard", 78, 96, 60, 500));
             ...
          }
          

          这样做 20 次,它会在你的牌组中装满 20 张独特的牌。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-22
            相关资源
            最近更新 更多