Java 如何遍历对象的ArrayList来创建条件?

Java 如何遍历对象的ArrayList来创建条件?,java,arraylist,bluej,Java,Arraylist,Bluej,我试图让我的最后一个IF循环遍历其中的三个对象 在PinballTypeOne列表中,只有ArrayList中的这三个对象在碰到左侧墙后,才应将其以前的颜色更改为橙色。碰撞确实起作用,但对象一直在反弹,而没有被更改为橙色,因此我认为ArrayList是如何生成的,或者我是如何尝试迭代ArrayList的。没有编译错误 public class PinballObject { private int currentXLocation; private int currentY

我试图让我的最后一个IF循环遍历其中的三个对象 在PinballTypeOne列表中,只有ArrayList中的这三个对象在碰到左侧墙后,才应将其以前的颜色更改为橙色。碰撞确实起作用,但对象一直在反弹,而没有被更改为橙色,因此我认为ArrayList是如何生成的,或者我是如何尝试迭代ArrayList的。没有编译错误

   public class PinballObject
{
    private int currentXLocation;
    private int currentYLocation;
    private int speedXTravel;
    private int speedYTravel;
    private Color colour;
    private int radius;
    private Machine machine;
    private final int leftWallPosition;
    private final int bottomWallPosition;
    private final int topWallPosition;
    private final int rightWallPosition;
    private ArrayList<PinballObject> PinballTypeOneList = new ArrayList<PinballObject>();




    /**
     * Constructor for objects of class Pinball_Obj
     * 
     * @param xPos  the horizontal coordinate of the object
     * @param yPos  the vertical coordinate of the object
     * @param xVel  the horizontal speed of the object
     * @param yVel  the vertical speed of the object
     * @param objectRadius  the radius (in pixels) of the object
     * @param objectColor  the color of the object
     * @param theMachine  the machine this object is in
     */
    public PinballObject(int xPos, int yPos, int xVel, int yVel, Color objectColor, int objectRadius, Machine theMachine)
    {
        currentXLocation = xPos;
        currentYLocation = yPos;
        speedXTravel = xVel;
        speedYTravel = yVel;
        colour = objectColor;
        radius = objectRadius;
        machine = theMachine;
        leftWallPosition = machine.getLeftWall();
        bottomWallPosition = machine.getBottomWall();
        topWallPosition = machine.getTopWall();
        rightWallPosition = machine.getRightWall();
    }

    public void makeType1()
    {
        PinballObject firstTypeOne = new PinballObject(50, 200, -5, 3, Color.RED, 10, machine);
        PinballTypeOneList.add(firstTypeOne);

        PinballObject secondTypeOne = new PinballObject(100, 300, 1, 2, Color.BLUE, 55, machine);
        PinballTypeOneList.add(secondTypeOne);

        PinballObject thirdTypeOne = new PinballObject(450, 125, -1, -1, Color.YELLOW, 40, machine);
        PinballTypeOneList.add(thirdTypeOne);
    }


    /**
     * Move this object according to its position and speed and redraw.
     **/
    public void move()
    {
         // remove from universe at the current position
        machine.erase(this);

        // compute new position
        currentYLocation += speedYTravel;
        currentXLocation += speedXTravel;

        // check if it has hit the leftwall + change color to orange only for the objects IN the arraylist
        if(currentXLocation <= (leftWallPosition + radius)) 
        {
            currentXLocation = leftWallPosition + radius;
            speedXTravel = -speedXTravel;

            if(this.equals(PinballTypeOneList)){
                colour = Color.ORANGE;
            }

            // draw again at new position
         machine.draw(this);

        }
    }
公共类pinbalobject
{
私有内部位置;
私人住宅;
私人int speedXTravel;
私人快速旅行;
私人色彩;
私有整数半径;
私人机器;
私人最终职位;
私人最终职位;
私人最终职位;
私人最终职位;
私有ArrayList PinballTypeOneList=新ArrayList();
/**
*Pinball_Obj类对象的构造函数
* 
*@param xPos对象的水平坐标
*@param yPos对象的垂直坐标
*@param xVel对象的水平速度
*@param yVel对象的垂直速度
*@param objectRadius对象的半径(以像素为单位)
*@param objectColor对象的颜色
*@param机器此对象所在的机器
*/
公共Pinball对象(int xPos、int YPO、int xVel、int yVel、Color objectColor、int objectRadius、机器)
{
currentXLocation=xPos;
currentlocation=yPos;
speedXTravel=xVel;
speedYTravel=yVel;
颜色=对象颜色;
半径=对象半径;
机器=机器;
leftWallPosition=machine.getLeftWall();
bottomWallPosition=machine.getBottomWall();
topWallPosition=machine.getTopWall();
rightWallPosition=machine.getRightWall();
}
公共void makeType1()
{
PinballObject firstTypeOne=新PinballObject(50,200,-5,3,彩色,红色,10,机器);
PinballTypeOne列表。添加(第一个TypeOne);
PinballObject secondTypeOne=新的PinballObject(100300,1,2,彩色,蓝色,55,机器);
PinballTypeOne列表。添加(第二个TypeOne);
PinballObject thirdTypeOne=新PinballObject(450125,-1,-1,颜色为黄色,40,机器);
PinballTypeOne列表。添加(第三种类型);
}
/**
*根据其位置和速度移动此对象并重新绘制。
**/
公开作废动议()
{
//在当前位置从宇宙中移除
清除(这个);
//计算新位置
currentYLocation+=快速旅行;
currentXLocation+=speedXTravel;
//检查它是否击中了leftwall+仅将arraylist中的对象的颜色更改为橙色

如果(currentXLocation为您的功能移动,您应该这样做:

public void move()
{
    machine.erase(this);

    currentYLocation += speedYTravel;
    currentXLocation += speedXTravel;

    if(currentXLocation <= (leftWallPosition + radius)) 
    {
        currentXLocation = leftWallPosition + radius;
        speedXTravel = -speedXTravel;
        for(PinballObject po : PinballTypeOneList) { //Iterating through list of PinballObject's
            if(this.equals(po)) { // If this PinballObject is in list
                colour = Color.ORANGE; // Or use po.colour
            }
        }
        machine.draw(this);
    }
}
公共作废移动()
{
清除(这个);
currentYLocation+=快速旅行;
currentXLocation+=speedXTravel;

if(currentXLocation Your
if(this.equals(PinballTypeOneList))
将始终返回true。这意味着什么?这意味着反弹后它将始终是橙色的。这也意味着如果代码中有无用的
。问题是它们根本不会变为橙色,如果我删除该条件,我可以使它们全部变为橙色,但我尝试做的是只让ArrayList中的对象变为橙色橙色。它们在碰撞后反弹为相同的颜色。正如我前面提到的,我想我可能对ArrayList是如何制作的或者我是如何尝试迭代ArrayList有问题,我要么是瞎的,要么就是看不见。首先,你有一个类
pinbalObject
,它有一个构造函数和函数
move()
makeType1()
这个
返回
PinballObject
类,它不能等于
PinballTypeOneList
它是
PinballObject
的数组,所以你需要从数组中选择一个
PinballObject
,并使用
if
如果(this.equals(PinballTypeOneList))
返回始终为false。