GravityCheck
GravityCheck

Reputation: 13

What is wrong with the .add in my ArrayList?

I'm currently working on a simple game in java, representative of the DOS game Gorillas.bas. I'm creating an arraylist to store the individual buildings do to collision checking and whatnot, but Eclipse is giving me an error no matter how I go about it. Here is what i've got for the arraylist.

ArrayList<Rectangle> Buildings = new ArrayList<Rectangle>();
Point build1 = new Point(75,30);
Rectangle building1 = new Rectangle(build1, BUILDING_WIDTH, 150);
Buildings.add(Rectangle building1);

The error is on the .add() method, it tells me that the method needs a body instead of a semicolon. What does this mean? Is eclipse not recognizing the .add()?

EDIT: A bit of the code around it as requested; doesn't appear to have any syntax errors.

public double bananaX = 85; 
public double bananaY = 292;
public double bananaDX = 1; 
public double bananaDY = 1;
public double power = 0;
public double direction = 0;
public double rise;
public double run;
Point start = new Point(0,0);
Point finish = new Point(0,0);`

ArrayList<Rectangle> buildings = new ArrayList<Rectangle>();
Point build1 = new Point(75,350);
Point build2 = new Point(225, 250);
Point build3 = new Point(325, 200);
Point build4 = new Point(425, 200);
Point build5 = new Point(525, 250);
Point build6 = new Point(675, 350);
Rectangle building1 = new Rectangle(build1, BUILDING_WIDTH, 150);
buildings.add(building1);


public void power(Point start, Point finish)
{
    int power = 0;
    power = (int)start.distanceTo(finish);


}

public void direction(Point start, Point finish)
{
    double direction = 0;
    rise = (finish.y - start.y)*-1;
    run = (finish.x - start.x)*-1;
    direction = rise/run;
    bananaDX = run/10;
    bananaDY = (rise/10);
    System.out.printf("rise = %f\nrun = %f\ndirection = %f\n\n ",rise, run, direction);

}

Upvotes: 1

Views: 850

Answers (2)

Mike
Mike

Reputation: 8545

You just need to have:

Buildings.add(building1);

Since building1 is already a Rectangle. You have already created the Rectangle object above it so you only need to use the variable itself because it is of the correct type.

Edit: You should probably also rename Buildings buildings to avoid any confusion. When you name a variable with a capital letter it looks like a type and not a variable.

Edit2: Based on the code you provided, you need to have buildings.add(building1); inside of a method of some sort. You should create an initialize method that gets called at the start if you want to have it added in at the beginning.

Upvotes: 6

Corey
Corey

Reputation: 264

Don't double up on Rectangle.

Buildings.add(building1);

Upvotes: 3

Related Questions