关于:java.lang.ArrayIndexOutofBoundsException:6
我很难找到解决这个错误的方法。我似乎不明白为什么。顺便说一句,我是编程新手关于:java.lang.ArrayIndexOutofBoundsException:6,java,Java,我很难找到解决这个错误的方法。我似乎不明白为什么。顺便说一句,我是编程新手 public class Player{ private Map map; private Image player2; private double x; private double y; private double deltaX = 250; private double deltaY = 250; private int width; private int height; public Player
public class Player{
private Map map;
private Image player2;
private double x;
private double y;
private double deltaX = 250;
private double deltaY = 250;
private int width;
private int height;
public Player(){
player2 = new ImageIcon("C://images//ball.PNG").getImage();
x = 0;
y = 0;
deltaX = 100;
deltaY = 100;
}
public void movePlayer(long delta){
map = new Map();
if(x > width){
x = width;
//vertical = true;
deltaX = -deltaX;
}else if(x < 0){
x = 0;
deltaX = -deltaX;
}
else{
x += (delta * deltaX) / 1000;
}
//This one can't execute properly
if(map.getMap(x, y)) {
System.out.println("Collided");
}else{
}
}
public void draw(Graphics2D g){
g.drawImage(player2, (int)x, (int)y, null);
}
公共类播放器{
私人地图;
私人影像播放器2;
私人双x;
私人双y;
私人双重税收=250;
私人双三角洲=250;
私有整数宽度;
私人内部高度;
公共播放器(){
player2=newimageicon(“C://images//ball.PNG”).getImage();
x=0;
y=0;
deltaX=100;
deltaY=100;
}
公共播放器(长三角){
map=新map();
如果(x>宽度){
x=宽度;
//垂直=真;
deltaX=-deltaX;
}else如果(x<0){
x=0;
deltaX=-deltaX;
}
否则{
x+=(增量*增量)/1000;
}
//这个不能正常执行
if(map.getMap(x,y)){
System.out.println(“冲突”);
}否则{
}
}
公共作废图纸(图形2D g){
g、 drawImage(播放器2,(整数)x,(整数)y,空);
}
}
这是Map类:
public class Map{
private Image tile1;
private int map[][] = {{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 0},
};
public Map(){
tile1 = new ImageIcon("C://images//tile1.PNG").getImage();
}
public boolean getMap(double x, double y){
return map[(int)x][(int)y] == 0;
}
public void draw(Graphics2D g){
for(int x = 0; x < map.length; x++){
for(int y = 0; y < map.length; y++){
if(map[x][y] == 0)
g.drawImage(tile1, x*33, y*33, null);
}
}
}
公共类映射{
私有图像块1;
私有整数映射[][]={{1,1,1,1,1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 0},
};
公共地图(){
tile1=新图像图标(“C://images//tile1.PNG”).getImage();
}
公共布尔getMap(双x,双y){
返回映射[(int)x][(int)y]==0;
}
公共作废图纸(图形2D g){
对于(int x=0;x
}
我希望有人能指出我哪里做错了。。。谢谢没有检查,
=0&&Attach堆栈跟踪,如果可能,还有行号。这是家庭作业吗?如果是这样的话,它应该被标记为这样的。您可能会使用大于4或小于0的x或y值调用getMap。。。你能在if(map.getMap(x,y)){
行之前检查x和y的值吗?@assylias,这不是一个家庭作业,而是一个伪代码。我正在尝试解决这个问题,获取逻辑并将其应用到我的工作中。无论如何,非常感谢!嗨,伙计们!感谢你们的输入。我做了什么(如你所建议的),用于检查我的x和y的位置。由于我的x和y被声明为双精度,我尝试将x和y值与图像大小的值分开:if(map.getMap(x/33,y/33)==0){System.out.println(“碰撞”);}else{}这是可行的,但我仍然需要对我的角色x和y的位置做一些调整。我将更新这篇文章。
if(map.getMap(x, y)) {
for(int x = 0; x < map.length; x++){
for(int y = 0; y < map.length; y++){
for(int x = 0; x < map.length; x++){
for(int y = 0; y < map[x].length; y++){
//^^^