I have two classes, Entity and Ship where Ship extends Entity. In my entity class I have a private field speed. In my ship class I have a update method that is simply trying to increment speed by saying current speed + acceleration times a delta time.
Here is Entity:
private float height, rotation, speed, width;
private Sprite sprite;
private Vector2 origin, position;
public Entity (float x, float y) { position = new Vector2 (x, y); }
public Entity (float x, float y, Texture texture)
{
this (x, y);
if (texture != null)
{
sprite = new Sprite (texture);
width = sprite.getWidth ();
height = sprite.getHeight ();
origin = new Vector2 (x + width / 2f, y + height / 2f);
}
else
{
sprite = null;
origin = position;
}
}
public Entity (float x, float y, float rotation, Texture texture)
{
this (x, y, texture);
this.rotation = rotation;
}
public Entity (float x, float y, float rotation, float speed, Texture texture)
{
this (x, y, rotation, texture);
this.speed = speed;
}
public float getHeight () { return height; }
public float getRotation () { return rotation; }
public float getSpeed () { return speed; }
public float getWidth () { return width; }
public float getX () { return position.x; }
public float getY () { return position.y; }
public void setPosition (Vector2 vector) { position = vector; }
public void setRotation (float value) { rotation = value; }
public void setSpeed (float value) { speed = value; }
public void setSprite (Sprite sprite) { this.sprite = sprite; }
public void setX (float value) { position.x = value; }
public void setY (float value) { position.y = value; }
public Sprite getSprite () { return sprite; }
public Vector2 getOrigin () { return origin; }
public Vector2 getPosition () { return position; }
And here is the update()
method of Ship:
setSpeed (getSpeed () + acceleration * delta);
System.out.println (getSpeed ());
What I expect to happen is that speed is increased by whatever acceleration * delta is. However, what happens is that speed is set equal to that value, not incremented. If I set the fields in Entity to static then my code works, but I feel that I shouldn't do that. I've also tried setting the fields to protected and the Entity class to abstract but I still get the same effect. I'm really at a loss as to what I'm doing wrong, unless setting the fields to static is correct?
EDIT: The entire update method of Ship.
public void update (float delta)
{
// Updating the origin's position
getOrigin ().set (getX () + getWidth () / 2f, getY () + getHeight () / 2f);
updateTurrets (delta);
updateBullets (delta);
setSprite (updateSprite (delta));
//calculateRotateTo (moveTo);
setRotation (0f);
rotateTo = getRotation ();
if (getRotation () != rotateTo)
{
setRotation (rotateTo);
}
else if (getOrigin () != moveTo)
{
/*float currDistance = Utils.calculateDistance (originalPos, getOrigin ());
float totDistance = Utils.calculateDistance (originalPos, moveTo);
if (currDistance >= totDistance / 2f)
accelerating = false;
else
accelerating = true;
if (accelerating)
setSpeed (getSpeed () + acceleration * delta);
else
{
if (getSpeed () > 0f)
setSpeed (getSpeed () - acceleration * delta);
else
{
setSpeed (0f);
setPosition (moveTo);
}
}*/
setSpeed (getSpeed () + acceleration * delta);
System.out.println (getSpeed ());
}
}
EDIT #2: I did some extra testing and if I put a ship instance in the main render() method that LibGDX gives it works. Anywhere else however, it doesn't.
I believe I fixed it or at least as far as I can tell. Apparently saying static Ship ship = new Ship ();
allowed for the fields to properly change without those fields also needing to be static.