Help, Please!

I have recently been having this problem within my code (java) where subclasses of certain classes share variables with every other instance.

Ex.

public abstract class Entity
{
protected int x, y;
protected int xvel, yvel;

public void setVel(int x, int y)
{
xvel = x;
yvel = y;
}
}

public class Grass extends Entity
{
public void update()
{
x += xvel;
y += yvel;
}
}

in my level class I have a list of Grass objects and so I update them in a for loop, but what I notice is that if I do the following:

entities.get(0).setVel(10, 10);

every single grass object has its velocity set to 10, 10

Please help. I have been struggling with this for 2 days.

Comments

avh4
19. Aug 2013 · 01:22 UTC
What you’re describing should only be the case if xvel and yvel are declared as “static”, but the code above doesn’t have that. You must be setting the velocity of the other entities without realizing it. I’d be trying to set a breakpoint in setVel and see where (and with which objects) it’s actually getting called.
Adjotur
19. Aug 2013 · 01:23 UTC
Honestly I’m no expert at all, but I’ve had similar problems to this, and I think it may have to do with the creation of your entities in the list. Again, I’m still new to Java, but I think what could possibly be happening is they’re all the same object, if that makes any sense. What I’m trying to say is, is it possible that you have created one object, then inserted it into an array multiple times?
19. Aug 2013 · 01:32 UTC
Without seeing more of the code, I’d agree with Adjotur – what you think are multiple instances are all actually different references to one instance. Your debugger should give you an object ID, look at your list entries in the debugger and make sure the object ID is different for each entry in the list. Then take a look at how you’re creating and adding your entries to the list.
19. Aug 2013 · 01:36 UTC
Once you eliminate what’s impossible, whatever’s left, however improbable, is the truth.
goodpaul6
19. Aug 2013 · 02:02 UTC
That is a test to see whether setting the velocity of one instance sets the velocity of others.
19. Aug 2013 · 02:08 UTC
Ah ok, I see the problem, I think. First, check in the debugger that that one line, setVelocity(10, 10), indeed does cause the immediate setting of other objects’ velocities.
goodpaul6
19. Aug 2013 · 02:10 UTC
YOUUU ARE FREAKINGTHE MAN DUWDJXSofjaseoifsiodfj ahsfhsifhsa
19. Aug 2013 · 02:13 UTC
Glad I could help =D
goodpaul6
19. Aug 2013 · 02:17 UTC
I gotta submit a bug or something on that in the library because I used Vector2.Zero basically everywhere, and I don’t want anyone else to go through what I had to. Good thing eclipse has a restore from history utility, otherwise I wouldeve been here for a while.
19. Aug 2013 · 02:20 UTC
Given that you used it everywhere, I’m astonished that you’ve had so very problems thus far.

I’d just use Eclipse’s global replace and replace all of the Vector2.Zero’s to new Vector2(0,0)’s.
goodpaul6
19. Aug 2013 · 01:53 UTC
Looking at the debugger, all I see is that setVel is being called on one instance, the velocity is being set on each instance.
goodpaul6
19. Aug 2013 · 02:02 UTC
The velocity variables are acting as if they are static