数组中的NPC运动

时间:2011-12-01 13:12:19

标签: arrays multidimensional-array

我正在做一个数组游戏,下面是我的Board类,它绘制阵列并在11,11产生5个猎人,我的'route1'方法应该是每当玩家移动时移动猎人的方法,但是我的hunter.x和hunter.y在每次重新涂漆后保持11,我该如何解决这个问题?

  public class Board { 

    public String emptyfield = "-";
    public  String [][]a2 = new String[12][12];
 public Hunter hunters[] = new Hunter[5];

public void paint(){

    int numHunters =5 ;
    for (int i =0; i < numHunters; i ++){
        hunters[i] = new Hunter(11,11,"H");
    }
Player player = new Player();
        for (int r = 0 ; r < a2.length; r++){
            for (int c= 0; c <a2[r].length; c++){
                a2 [r][c] = emptyfield;
                a2[Player.x][Player.y] = Player.name;
                for (int i = 0; i < numHunters; i++){
                     Hunter h = hunters[i];
                     a2[h.x][h.y]=h.name;

                }
                System.out.print(" "+a2[r][c]);
            }
            System.out.println("");
        }
        System.out.println("     Strength:  "+Player.hp);System.out.println("      Score  "+Player.score);

    }

public void route1(){
    Board board = new Board();
    Hunter Hunter = new Hunter(11,11,"H");
    Scanner in = new Scanner(System.in);
    Random number = new Random(2);
    int random = number.nextInt(2);
     if(random ==1)
        Hunter.x = Hunter.x -1;
    else
       Hunter.y = Hunter.y -1;
    }

1 个答案:

答案 0 :(得分:0)

如果我正确地阅读了这个内容,那么每次调用油漆时,你都会在11,11位重新创建所有猎人。

public void paint(){

    int numHunters =5 ;
    for (int i =0; i < numHunters; i ++)
    {
        hunters[i] = new Hunter(11,11,"H");
    }

每次调用paint时,此代码都会替换您的猎人数组,从而删除稍后在代码中所做的任何更改。你需要移动(新的Hunter(11,11,“H”)到一个只调用一次的地方。