简体   繁体   English

尝试从 Java 中的方法返回 float[]

[英]Attempting to return a float[] from a method in Java

I'm writing a game and working on collisions.我正在编写游戏并处理碰撞。 Currently, I'm trying to use the tileCollision function to return a 1-dimensional array containing 2 numbers;目前,我正在尝试使用 tileCollision 函数返回一个包含 2 个数字的一​​维数组; the coordinates of the corner.角的坐标。 (represented in the program by cX and cY) (在程序中由 cX 和 cY 表示)

I expected this to run smoothly: that is, to return {-999, -999} if it wasn't colliding, and to return a valid set of coordinates otherwise.我希望这能顺利运行:也就是说,如果没有碰撞,则返回 {-999, -999} ,否则返回一组有效的坐标。 Unfortunately, IntelliJ tells me that the goodCX and goodCY variables "might not have been initialised" which I do not know how to solve.不幸的是,IntelliJ 告诉我 goodCX 和 goodCY 变量“可能尚未初始化”,我不知道如何解决。 Running the program gives me the same error.运行程序给了我同样的错误。

The Tile class can be treated as a class containing an X value, a Y value, and a texture.可以将 Tile 类视为包含 X 值、Y 值和纹理的类。 The t.texture.width & t.texture.height should be set to 50 to make it simpler to understand. t.texture.width & t.texture.height 应设置为 50 以使其更易于理解。 Entity can be assumed as a class containing variables named x, y, vx, vy, and a "texture" whose dimensions should be 20x20.实体可以被假定为一个包含名为 x、y、vx、vy 的变量和一个尺寸应该为 20x20 的“纹理”的类。 I use Java with the Processing 3 library to render my code and am happy to provide extra info if needed.我使用 Java 和 Processing 3 库来呈现我的代码,如果需要,我很乐意提供额外的信息。

The main problem is less with those rather than the "might not have been initialised" problem, though.不过,主要问题不在于那些,而不是“可能尚未初始化”的问题。

Please, go easy on me, and if possible, don't use anything wildly outside the realm of what is demonstrated in my code.请对我放轻松一点,如果可能的话,不要在我的代码中展示的范围之外疯狂使用任何东西。 I am an amateur programmer, and while I have been programming for a while, am not exactly professional.我是一名业余程序员,虽然我已经编程了一段时间,但并不完全专业。

Here's my code:这是我的代码:

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); }
}

The problem resides in the fact that you only assign (and first-assign, and hence, initialize) your goodCX and goodCY variables inside a complex if nested inside your for loops.问题在于, if嵌套在for循环中,您只能在复合体中分配(并首先分配,因此初始化)您的goodCXgoodCY变量。 Then, if(isCollide) , you will attempt to return them, but there is no way the compiler can infer any kind of connection between the complex if condition you have, and the isCollide condition, so you may be returning unassigned references.然后, if(isCollide) ,您将尝试返回它们,但编译器无法推断您拥有的复杂if条件与isCollide条件之间的任何类型的联系,因此您可能会返回未分配的引用。

To resolve this, simply make a default initialization of your float references at the top, as follows:要解决此问题,只需在顶部对浮动引用进行默认初始化,如下所示:

float goodCX = 0.0;
float goodCY = 0.0;

If the collision check do not evaluate true GoodCX and GoodCY will never be initialized.如果碰撞检查不评估真正的 GoodCX,则 GoodCY 将永远不会被初始化。 You will have problems trying to return "new float[]{goodCX, goodCY}".尝试返回“new float[]{goodCX, goodCY}”时会遇到问题。 Try initializing your GoodCX and GoodCY with -999.尝试使用 -999 初始化您的 GoodCX 和 GoodCY。 You will also be able to simplify your code with that:您还可以通过以下方式简化代码:

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};
}

Intellij is giving you that response because the variables are declared but not initialized. Intellij 给你这个响应,因为变量被声明但没有初始化。 You need to define them with some default state or modify the code that follows to ensure they're initialized.您需要使用一些默认状态定义它们或修改后面的代码以确保它们已初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM