我如何遍历对象的ArrayList以创建条件?
问题描述:
我想要让我的最后一个IF循环遍历PinballTypeOne列表中的三个对象,只有在ArrayList中的这三个对象在它们碰到左墙后才能将它们以前的颜色更改为橙色。碰撞确实有效,但对象不会变为橙色,因此我认为我可能对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);
}
}
答
为了您的功能打动你应该这样做:
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);
}
}
,你需要覆盖,因为你使用自己定义的类(PinballObject)等于和的hashCode方法,请参阅this question。
我对你的问题PinballTypeOneList为什么它在PinballObject
类?这意味着每一个PinballObject
都有它,这听起来很糟糕...您需要制作PinballTypeOneList global然后在其中存储您的PinballObject
s,或者您有特殊的理由这样做?
在此之后,我相信你的代码应该工作(虽然我没看到大部分代码)。
您的if(this.equals(PinballTypeOneList))'将始终返回true。 – BladeMight
这是什么意思? – Heliangs
这意味着反弹后它将始终为橙色。这也意味着你在代码中没有任何'if'。 – BladeMight