I am making a game where random obstacles appear. The obstacles will move towards you. I currently have this code to animate the obstacles.

for (int i = 0; i < obstacle.size(); i++){ // animate walls
	obstacle.elementAt(i).x -= 1; 
}

My vector multiple instances of the Walls class

Walls w = new Walls ();
Vector<w> obstacle = new Vector<w> (5,5);

and this is what the Walls class looks like

import java.awt.Rectangle;

class Walls {
	Walls (){
		randomLocation ();
		
	}
	public void randomLocation() {
		// TODO Auto-generated method stub
		x = 510;
		y = (int)Math.round (Math.random ()*300);
		makeRect ();
	}
	private void makeRect() {
		// TODO Auto-generated method stub
		rect = new Rectangle (x,y,10,30);
	}
	public int x = 510;
	public int y;
	public Rectangle rect;
	
	
}

Eclipse gave me an error if my class that draws the objects didn't look like this

public class CopterG<w> extends JPanel implements KeyListener, ActionListener{
...
...
}

I am not sure what the <w> does in the drawing class.

I am trying to communicate from the class that draws the images (also animates them) with the Walls class which stores the positions of the walls. I don't know how I could fix the issue with the animation. It underlines the .x in the first code. I have made a snake game that works kind of like this, but that didn't give me any errors. I tried comparing the two and they are extremely similar.

If you would like me to re explain it maybe more clearly then I will.

Thanks for any help.

so, you do write the code but you just don't know what it means or what it is supposed to do?

I think what you ment to do was:

Vector<Walls> obstacle = new Vector<Walls>(5,5);

the name between < > shows what kind of elements you can put in the Vector, it's called Generics. you've put 'w', what isn't a class, so it goes searching for a x variable in a w class, which it won't find, which is why it gives you that error.

for a better object oriented design, you might want to declare those variables as private, and access them through getters and setters.

This is the first time I made a vector of classes. I knew that I needed to add an argument to the vector, I just didn't know which one to use. I do not usually write code if I don't know what it means, except this time it gave me an error and I wanted to try to fix it. Also I was confused with the <> at this part

public class CopterG<w> extends JPanel implements KeyListener, ActionListener{
...
...
}

What is the purpose of the <w> in that piece of code?

the same as with the Vector. and it's optional to make your code generic, it's not mandatory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.