简体   繁体   English

Java引用单个ArrayList实例

[英]Java referencing to single ArrayList instance

I have encountered a problem with the ArrayList I'm using. 我正在使用的ArrayList遇到问题。 This is a list of objects containing sprites with a color and location that differ from each other. 这是一个包含精灵的对象列表,这些精灵的颜色和位置互不相同。 The list is returned successfully and I can use a foreach loop to draw them all to the screen. 该列表已成功返回,我可以使用foreach循环将它们全部绘制到屏幕上。 After that I am checking if someone has clicked on their location and I want to return the color that the object contains. 之后,我要检查是否有人单击了他们的位置,并且我想返回对象包含的颜色。 The problem is I always get the color green back. 问题是我总是使颜色变绿。 Probably because I reference to all the objects instead of a single one. 可能是因为我引用了所有对象,而不是单个对象。 I have searched through this site and found similar problems and tutorials. 我搜索了该站点,发现了类似的问题和教程。 But I can not seem to find the information I need to fix the code so that I can reference to a single instance in the list while using a foreach loop to check them all. 但是我似乎找不到修复代码所需的信息,以便可以在使用foreach循环检查所有实例时引用列表中的单个实例。

After reading the answers and using break and the forgotten secondColor = true, I still have the same problem. 阅读答案并使用break和被遗忘的secondColor = true之后,我仍然遇到相同的问题。 Even when sending the clicked object to another function my problem is not solved. 即使将单击的对象发送到另一个功能时,我的问题也无法解决。 So instead of only showing the code for reading out the list I show the code for when the list is filled as well. 因此,我不仅仅显示用于读取列表的代码,还显示了列表填充时的代码。 How can I fix that I only refer to the green object at all times when reading the list but when using the for loop to draw I get every color???? 如何解决在阅读列表时始终只引用绿色对象,但是使用for循环绘制时却得到所有颜色的问题?

for(KleurWissel i : gameManager.getColorsList())
        {
            i.colorRectangle.draw(batch);
        }
        //Welke kleuren kies je?
        if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
            Iterator<KleurWissel> iterator = gameManager.getColorsList().iterator(); 
            while(iterator.hasNext()) 
            {
                KleurWissel item = (KleurWissel) iterator.next();

                if(item.getRectangle().contains(input.x, input.y)) 
                { 
                    if(firstColor == true && secondColor == false && item.selected == false)
                    {
                        colorArraySecond = item.colorArray;
                        colorArrayUsed = colorArrayFirst;
                        colorSelected = true;
                        item.selected = true;
                        secondColor = true;
                        break;
                    }
                    if(firstColor == false && item.selected == false){
                        colorArrayFirst = item.colorArray;
                        firstColor = true;
                        item.selected = true;
                        break;
                    }
                }
            }  
        }

And here the code for filling the list. 这是用于填充列表的代码。

private int[] colorsRGB = {0, 50, 150, 150, 200, 255};
float[] colorArrayUsed = new float[4];
private ArrayList<KleurWissel> colorsList = new ArrayList<KleurWissel>();


public void setColorsList()
{
    Vector2 tempV;
    KleurWissel temp;
    for(int i = 0; i < 6; i++)
    {
        if(i<3)
        {
            colorArrayUsed[0] = colorsRGB[5-i];
            colorArrayUsed[1] = colorsRGB[0+i];
            colorArrayUsed[2] = 0;
            colorArrayUsed[3] = 1;
            tempV = new Vector2(Gdx.graphics.getWidth()/2 - 150 + i * 150, Gdx.graphics.getHeight()/2 + 130);
        }
        else{
            if(i == 3)
            {
                colorArrayUsed[0] = 0;
                colorArrayUsed[1] = 0;
                colorArrayUsed[2] = colorsRGB[5];
                colorArrayUsed[3] = 1;
                tempV = new Vector2(Gdx.graphics.getWidth()/2 - 150, Gdx.graphics.getHeight()/2);
            }
            else{
                colorArrayUsed[0] = 0;
                colorArrayUsed[1] = colorsRGB[0+i];
                colorArrayUsed[2] = colorsRGB[5-i];
                colorArrayUsed[3] = 1;
                tempV = new Vector2(Gdx.graphics.getWidth()/2 - 600 + i * 150, Gdx.graphics.getHeight()/2);
            }

        }
        temp = new KleurWissel(colorArrayUsed, tempV);

        colorsList.add(temp);
    }
}

public ArrayList<KleurWissel> getColorsList()
{
    return colorsList;
}

KleurWissel is the color object, sorry for the bit of Dutch >.>. KleurWissel是彩色对象,对于荷兰语>。>有点抱歉。

Edited based on the comments: 根据评论进行编辑:

The libGDX Rectangle class has a contains method, and it seems like this is what you are checking there manually. libGDX Rectangle类有一个contains方法,看来这是您在此处手动检查的内容。 You could also print some information that tells you more about whether an object was hit or not. 您还可以打印一些信息,以告诉您有关物体是否被击中的更多信息。

It seems like the intended process is the following: 似乎预期的过程如下:

  • When the first item is clicked, its color should be stored as colorArrayFirst 单击第一个项目时,其颜色应存储为colorArrayFirst
  • When the second item is clicked, its color should be stored as colorArraySecond , and the colorSelected flag should be set to true 单击第二项时,其颜色应存储为colorArraySecond ,并且colorSelected标志应设置为true

The followign code shows how this could be done. 后续代码显示了如何完成此操作。 But the question still when this information will be "reset". 但是问题仍然是何时“重置”此信息。 That is, when will both arrays be reset to null , and when will the colorSelected flag be reset to false ? 也就是说,什么时候将两个数组都重置为null ,何时将colorSelected标志重置为false

private boolean colorSelected = false;
private int colorArrayFirst[] = null;
private int colorArraySecond[] = null;

void someGameLoopMethod()
{
    if(Gdx.input.isButtonPressed(Input.Buttons.LEFT))
    {
        Iterator<ColorObject> iterator = gameManager.getColorsList().iterator();
        while(iterator.hasNext())
        {
            ColorObject item = (ColorObject) iterator.next();

            float x = Gdx.input.getX();
            float y = Gdx.input.getY();
            Rectangle r = item.colorRectangle;

            System.out.println("Check if "+x+" "+y+" is in "+r+": "+r.contains(x,y));

            if (r.contains(x,y))
            {
                clickedItem(item);
            }
        }
    }
}


private void clickedItem(ColorObject item)
{
    if (colorArrayFirst == null)
    {
        colorArrayFirst = item.colorArray;
    }
    else if (colorArraySecond == null)
    {
        colorArraySecond = item.colorArray;
        colorSelected = true;
    }
}

// TODO Think about when this method should be called
private void resetClickingInformation()
{
    colorSelected = false;
    colorArrayFirst = null;
    colorArraySecond = null;
}

The logic in your original question is fine, except that you need to add either 您原始问题中的逻辑很好,只是您需要添加两个

secondColor = true;
break;

inside the block with if(firstColor == true && secondColor == false) . 在块内部使用if(firstColor == true && secondColor == false)

If you don't do this, then after you set the second colour, you will keep iterating through the ColorObject objects, looking for more of them that include the point that was clicked, until eventually, you get to the last one - which presumably spans the whole canvas and has green as its colour. 如果您不这样做,那么在设置第二种颜色之后,您将继续遍历ColorObject对象,寻找更多包含被单击点的对象,直到最终找到最后一个-大概是最后一个横跨整个画布,颜色为绿色。

The break; break; isn't strictly necessary, but it saves you from having redundant iterations. 并非绝对必要,但这可以避免重复迭代。

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

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