[英]not Able to add elements to an ArrayList
i'm still a beginner in java , i am trying to make a pacman game so i tried to make grid to let monesters follow the player , so i made a list called nieghbours contains all the nieghbouring squares to the current square of the monester but when i had a problem first when adding squares to the nieghbours Array list : 我仍然是Java的初学者,我正在尝试制作吃豆子游戏,所以我尝试制作网格以使monesters跟随玩家,因此我列出了一个名为nieghbours的列表,其中包含与monester的当前正方形相同的所有nieghbouring正方形,但是当我在向nieghbours Array列表添加正方形时首先遇到问题时:
i have a method called chase to update the x,y of the monester before rendering 我有一个称为追逐的方法来在渲染之前更新monester的x,y
i have a method called findNieghbours called in the chase method to find the nieghbours of the current square of the monester . 我有一个称为findNieghbours的方法,该方法在追逐方法中被调用,以查找monester当前广场的nieghbours。
but an error apear that the nieghbours ArrayList is empty ( has size of 0 ) . 但是出现错误,表明邻居阵列列表为空(大小为0)。
public ArrayList<Square> findNieghbours(){
ArrayList<Square> nieghbours=new ArrayList();
int currentX=current.getX();
int currentY=current.getY();
if(currentX<29&&!grid.get(currentY/30).get(currentX/30+1).isWall()){
nieghbours.add(grid.get(currentX/30+1).get(currentY/30));
}
if(currentX>0&&!grid.get(currentY/30).get(currentX/30-1).isWall()){
nieghbours.add(grid.get(currentX/30-1).get(currentY/30));
}
if(currentY<29&&!grid.get(currentY/30+1).get(currentX/30).isWall()){
nieghbours.add(grid.get(currentX/30).get(currentY/30+1));
}
if(currentY>0&&!grid.get(currentY/30-1).get(currentX/30).isWall()){
nieghbours.add(grid.get(currentX/30).get(currentY/30-1));
}
return nieghbours;
public void Chase(Square chasedSquare){
ArrayList<Square> nieghbours=findNieghbours();
Square lowest=nieghbours.get(0);
for(Square square : nieghbours){
if((square.getX()-chasedSquare.getX()) * (square.getX()-chasedSquare.getX()) + ( square.getY()-chasedSquare.getY()) * ( square.getY()-chasedSquare.getY()) < (lowest.getX()-chasedSquare.getX()) * (lowest.getX()-chasedSquare.getX()) + (lowest.getY()-chasedSquare.getY()) * lowest.getY()-chasedSquare.getY()){
lowest=square;
}
}
current=lowest;
}
the error apears at this line in chase method : 在追逐方法中此行出现错误:
Square lowest=nieghbours.get(0);
he said it is empty has size 0 他说它是空的,大小为0
but when i change the third condition to : 但是当我将第三个条件更改为:
if(currentY/30<29&&!grid.get(currentX/30+1).get(currentY/30+1).isWall()){
nieghbours.add(grid.get(currentX/30).get(currentY/30+1));
}
it works but not as i want ( in each corner it jumps in two steps to become on the the right of the player ) 它起作用但并非我想要的(在每个角落它都会跳两步成为玩家的右边)。
I suppose the grid is size 30 by 30. Is this correct? 我想网格的大小是30 x30。这是正确的吗?
Then currentX/30
and currentY/30
would be always 0
. 那么
currentX/30
和currentY/30
将始终为0
。 Is this what you intended? 这是您想要的吗?
Did you mean currentX%30
and currentY%30
instead? 您是说
currentX%30
和currentY%30
吗?
If you want to go over to the left side, when hitting the right side, you should use (currentX+1)%30
. 如果要转到左侧,则在击打右侧时,应使用
(currentX+1)%30
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.