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

问题描述 投票:0回答:1

我正在编写游戏并研究碰撞。目前,我正在尝试使用tileCollision函数返回包含2个数字的1维数组;角的坐标。 (在程序中由cX和cY表示)

我希望它运行平稳:也就是说,如果不发生冲突,则返回{-999,-999},否则返回一个有效的坐标集。不幸的是,IntelliJ告诉我,我可能不知道如何解决goodCX和goodCY变量“可能尚未初始化”。运行程序会给我同样的错误。

Tile类可以被视为包含X值,Y值和纹理的类。 t.texture.width和t.texture.height应该设置为50,以使其更易于理解。实体可以假定为包含名为x,y,vx,vy的变量和尺寸应为20x20的“纹理”的类。我将Java与Processing 3库一起使用来呈现我的代码,并很高兴在需要时提供额外的信息。

但是,主要问题在于那些问题,而不是“可能尚未初始化”问题。

[请对我放松,如果可能的话,请不要在我的代码所演示的范围之外疯狂使用任何内容。我是一名业余程序员,虽然我已经进行了一段时间编程,但并不完全专业。

这是我的代码:

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); }
}
java collision-detection
1个回答
2
投票

问题在于,您仅在嵌套在goodCX循环内的复杂goodCY中分配(并首先分配并初始化)您的iffor变量。然后,if(isCollide),您将尝试返回它们,但是编译器无法推断出您具有的复杂if条件与isCollide条件之间的任何类型的连接,因此您可能正在返回未分配的引用。

要解决此问题,只需对顶部的float引用进行默认初始化,如下所示:

float goodCX = 0.0;
float goodCY = 0.0;

0
投票

Intellij给您该响应,因为变量已声明但未初始化。您需要使用一些默认状态定义它们,或修改后面的代码以确保它们已初始化。


0
投票

如果冲突检查不评估为真,则GoodCX和GoodCY将永远不会初始化。您将遇到尝试返回“ 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};
}
© www.soinside.com 2019 - 2024. All rights reserved.