is this possible? I really need multiple instances of the objects I'm creating and when I make them static all of them share x and y coordinates and it gets annoying, does anyone know how to use variables in another class without making them static
Remove the static keyword and access them as instance members of the class?
Post some code so we can see what you're trying to do.
Image img;
static Rectangle rec;
static int x;
static int y;
if(up) {
//y-=1;
//rec.y-=1;
if(!rec.intersects(tObject.rec)) {
tObject.y+=1;tObject.rec.y+=1;
} if(rec.intersects(tObject.rec)) {
tObject.y-=1;tObject.rec.y-=1;
}
public tObject o = new tObject(500, 150, 20, 100, "images/");
public tObject t = new tObject(500, 270, 100, 20, "images/");
Make the fields non-static so their values can vary per instance.
Make the fields non-static so their values can vary per instance.
Cannot make a static reference to the non-static field tObject.rec
int getX(){
return x;
}
tObject.rec.getX();
Yah but it has to be static or I get an error, that's what I'm asking, is there any way around it?
Code:Cannot make a static reference to the non-static field tObject.rec
of course I could instead of tObject.rec.y use thing.e.rec.y, but then I would need to do that for every instance of tObject I created
You need to create an INSTANCE of the class. Classname variableName = new ClassName();.
public tObject o = new tObject(500, 150, 20, 100, "images/");
public tObject t = new tObject(500, 270, 100, 20, "images/");[/code]
He says that at the bottom of the post. It's an 'object' anyways not a 'class' in this context. The only way this could be a problem is if the Rectangle hasn't been initialized.
So it's probably not that.
It would help if you posted the error that you get when the variable is not static - as it definitely shouldn't be static.