Reputation: 21
I am trying to have my last IF loop to iterate through the three objects within the PinballTypeOne list, only these three objects inside the ArrayList should change their former color to orange once they've hit the left wall. Collision does work, but objects keep bouncing off without being changed to orange so i think i might have a problem with either how the ArrayList was made or how i'm attempting to iterate through the ArrayList. No compilation errors.
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);
}
}
Upvotes: 2
Views: 89
Reputation: 2810
For your function move you should do so:
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);
}
}
and you need to override equals and hashCode methods because you using your own defined class(PinballObject), see this question.
And i have question about your PinballTypeOneList why it is in PinballObject
class? This means every PinballObject
has it, this sounds bad... You need to make PinballTypeOneList global and then store in it your PinballObject
s, or you have particular reason for doing so?
After this I believe your code should work.(though i didn't see most code).
Upvotes: 1