尝试从 Java 中的方法 return 一个 float[]
Attempting to return a float[] from a method in Java
我正在编写游戏并处理碰撞。目前,我正在尝试使用 tileCollision 函数 return 一个包含 2 个数字的一维数组;角的坐标。 (在程序中用cX和cY表示)
我预计这会 运行 顺利进行:也就是说,return {-999, -999} 如果没有碰撞,return 一组有效的否则坐标。不幸的是,IntelliJ 告诉我 goodCX 和 goodCY 变量 "might not have been initialised" 我不知道如何解决。 运行 程序给了我同样的错误。
Tile class 可以被视为包含 X 值、Y 值和纹理的 class。 t.texture.width & t.texture.height 应设置为 50 以使其更易于理解。实体可以假设为一个 class,包含名为 x、y、vx、vy 的变量,以及一个 "texture",其尺寸应为 20x20。我使用 Java 和 Processing 3 库来呈现我的代码,如果需要,我很乐意提供额外的信息。
不过,主要问题不是那些,而是 "might not have been initialised" 问题。
请放轻松,如果可能的话,不要在我的代码中演示的范围之外疯狂使用任何东西。我是一个业余程序员,虽然我已经编程了一段时间,但并不是很专业。
这是我的代码:
public float[] tileCollision(Tile t)
{
boolean isCollide = false;
float tX = t.eX;
float tX2 = t.eX + t.texture.width;
float tY = t.eY;
float tY2 = t.eY + t.texture.height;
float[] cX = new float[]{this.x + this.vx, this.x + this.texture.width + this.vx};
float[] cY = new float[]{this.y + this.vy, this.y + this.texture.height + this.vy};
float[] bad = new float[]{-999, -999};
float goodCX, goodCY;
System.out.println("\nCollisions Testing Names:");
System.out.println(this);
System.out.println(t);
for(int i = 0; i < 2; i ++)
{
for(int i_ = 0; i_ < 2; i_ ++)
{
System.out.println("\nCollisions Testing:");
System.out.println("Entity X: " + cX[i]);
System.out.println("Entity Y: " + cY[i_]);
System.out.println("Tile Xs: " + tX + ", " + tX2);
System.out.println("Tile Ys: " + tY + ", " + tY2);
if( ( (tX <= cX[i]) && (cX[i] <= tX2) ) && ( (tY <= cY[i_]) && (cY[i_] <= tY2) ) )
{
isCollide = true;
goodCX = cX[i];
goodCY = cY[i_];
}
}
}
System.out.println("Am I colliding?\n>>> " + isCollide);
if(isCollide)
{
return new float[]{goodCX, goodCY};
}
else { return(bad); }
}
Intellij 给您这样的响应是因为变量已声明但未初始化。您需要使用一些默认状态来定义它们或修改后面的代码以确保它们已初始化。
问题在于您只在嵌套在for
循环。然后,if(isCollide)
,您将尝试 return 它们,但是编译器无法推断出您拥有的复杂 if
条件与 isCollide
条件,因此您可能 returning 未分配的引用。
要解决此问题,只需在顶部对浮动引用进行默认初始化,如下所示:
float goodCX = 0.0;
float goodCY = 0.0;
如果碰撞检查不评估真正的 GoodCX 和 GoodCY 将永远不会被初始化。您在尝试 return "new float[]{goodCX, goodCY}" 时会遇到问题。尝试使用 -999 初始化 GoodCX 和 GoodCY。您还可以通过以下方式简化代码:
public float[] tileCollision(Tile t)
{
boolean isCollide = false;
float tX = t.eX;
float tX2 = t.eX + t.texture.width;
float tY = t.eY;
float tY2 = t.eY + t.texture.height;
float[] cX = new float[]{this.x + this.vx, this.x + this.texture.width + this.vx};
float[] cY = new float[]{this.y + this.vy, this.y + this.texture.height + this.vy};
float goodCX = -999;
float goodCY = -999;
System.out.println("\nCollisions Testing Names:");
System.out.println(this);
System.out.println(t);
for(int i = 0; i < 2; i ++)
{
for(int i_ = 0; i_ < 2; i_ ++)
{
System.out.println("\nCollisions Testing:");
System.out.println("Entity X: " + cX[i]);
System.out.println("Entity Y: " + cY[i_]);
System.out.println("Tile Xs: " + tX + ", " + tX2);
System.out.println("Tile Ys: " + tY + ", " + tY2);
if( ( (tX <= cX[i]) && (cX[i] <= tX2) ) && ( (tY <= cY[i_]) && (cY[i_] <= tY2) ) )
{
goodCX = cX[i];
goodCY = cY[i_];
}
}
}
return new float[]{goodCX, goodCY};
}
我正在编写游戏并处理碰撞。目前,我正在尝试使用 tileCollision 函数 return 一个包含 2 个数字的一维数组;角的坐标。 (在程序中用cX和cY表示)
我预计这会 运行 顺利进行:也就是说,return {-999, -999} 如果没有碰撞,return 一组有效的否则坐标。不幸的是,IntelliJ 告诉我 goodCX 和 goodCY 变量 "might not have been initialised" 我不知道如何解决。 运行 程序给了我同样的错误。
Tile class 可以被视为包含 X 值、Y 值和纹理的 class。 t.texture.width & t.texture.height 应设置为 50 以使其更易于理解。实体可以假设为一个 class,包含名为 x、y、vx、vy 的变量,以及一个 "texture",其尺寸应为 20x20。我使用 Java 和 Processing 3 库来呈现我的代码,如果需要,我很乐意提供额外的信息。
不过,主要问题不是那些,而是 "might not have been initialised" 问题。
请放轻松,如果可能的话,不要在我的代码中演示的范围之外疯狂使用任何东西。我是一个业余程序员,虽然我已经编程了一段时间,但并不是很专业。
这是我的代码:
public float[] tileCollision(Tile t)
{
boolean isCollide = false;
float tX = t.eX;
float tX2 = t.eX + t.texture.width;
float tY = t.eY;
float tY2 = t.eY + t.texture.height;
float[] cX = new float[]{this.x + this.vx, this.x + this.texture.width + this.vx};
float[] cY = new float[]{this.y + this.vy, this.y + this.texture.height + this.vy};
float[] bad = new float[]{-999, -999};
float goodCX, goodCY;
System.out.println("\nCollisions Testing Names:");
System.out.println(this);
System.out.println(t);
for(int i = 0; i < 2; i ++)
{
for(int i_ = 0; i_ < 2; i_ ++)
{
System.out.println("\nCollisions Testing:");
System.out.println("Entity X: " + cX[i]);
System.out.println("Entity Y: " + cY[i_]);
System.out.println("Tile Xs: " + tX + ", " + tX2);
System.out.println("Tile Ys: " + tY + ", " + tY2);
if( ( (tX <= cX[i]) && (cX[i] <= tX2) ) && ( (tY <= cY[i_]) && (cY[i_] <= tY2) ) )
{
isCollide = true;
goodCX = cX[i];
goodCY = cY[i_];
}
}
}
System.out.println("Am I colliding?\n>>> " + isCollide);
if(isCollide)
{
return new float[]{goodCX, goodCY};
}
else { return(bad); }
}
Intellij 给您这样的响应是因为变量已声明但未初始化。您需要使用一些默认状态来定义它们或修改后面的代码以确保它们已初始化。
问题在于您只在嵌套在for
循环。然后,if(isCollide)
,您将尝试 return 它们,但是编译器无法推断出您拥有的复杂 if
条件与 isCollide
条件,因此您可能 returning 未分配的引用。
要解决此问题,只需在顶部对浮动引用进行默认初始化,如下所示:
float goodCX = 0.0;
float goodCY = 0.0;
如果碰撞检查不评估真正的 GoodCX 和 GoodCY 将永远不会被初始化。您在尝试 return "new float[]{goodCX, goodCY}" 时会遇到问题。尝试使用 -999 初始化 GoodCX 和 GoodCY。您还可以通过以下方式简化代码:
public float[] tileCollision(Tile t)
{
boolean isCollide = false;
float tX = t.eX;
float tX2 = t.eX + t.texture.width;
float tY = t.eY;
float tY2 = t.eY + t.texture.height;
float[] cX = new float[]{this.x + this.vx, this.x + this.texture.width + this.vx};
float[] cY = new float[]{this.y + this.vy, this.y + this.texture.height + this.vy};
float goodCX = -999;
float goodCY = -999;
System.out.println("\nCollisions Testing Names:");
System.out.println(this);
System.out.println(t);
for(int i = 0; i < 2; i ++)
{
for(int i_ = 0; i_ < 2; i_ ++)
{
System.out.println("\nCollisions Testing:");
System.out.println("Entity X: " + cX[i]);
System.out.println("Entity Y: " + cY[i_]);
System.out.println("Tile Xs: " + tX + ", " + tX2);
System.out.println("Tile Ys: " + tY + ", " + tY2);
if( ( (tX <= cX[i]) && (cX[i] <= tX2) ) && ( (tY <= cY[i_]) && (cY[i_] <= tY2) ) )
{
goodCX = cX[i];
goodCY = cY[i_];
}
}
}
return new float[]{goodCX, goodCY};
}