【发布时间】:2017-08-12 05:06:34
【问题描述】:
我试图让我的最后一个 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);
}
}
【问题讨论】:
-
您的
if (this.equals(PinballTypeOneList))将始终返回 true。 -
这是什么意思?
-
这意味着它在反弹后总是橙色的。这也意味着你在代码中有无用的
if。 -
问题是它们根本不会变成橙色,如果我删除条件,我可以让它们全部变成橙色,但我想要做的只是让 ArrayList 中的对象变成橘子。它们在碰撞后反弹相同的颜色。正如我上面提到的,我认为我可能对 ArrayList 的制作方式或尝试遍历 ArrayList 的方式有疑问,我要么是盲人,要么就是看不到它。
-
首先你有一个类
PinballObject,它有一个构造函数和函数move()和makeType1()。this返回PinballObject类,它不能等于PinballTypeOneList这是PinballObject的数组,所以你需要select 以某种方式从数组中选择一个PinballObject并使用你的@987654332 @。if (this.equals(PinballTypeOneList))总是返回 false。