我正在尝试让我的最后一个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);
}
}
答案 0 :(得分:0)
对于你的功能移动你应该这样做:
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);
}
}
并且需要覆盖等于和 hashCode 方法,因为您使用自己定义的类(PinballObject),请参阅this question 。
我对你的 PinballTypeOneList 有疑问,为什么它在PinballObject
课程?这意味着每个PinballObject
都有它,这听起来很糟糕......你需要将 PinballTypeOneList 设为全局,然后将PinballObject
存储在其中,或者你有特殊的理由做所以?
在此之后我相信你的代码应该可行。(虽然我没有看到大多数代码)。