Java - 未正确更改超类字段
Java - Superclass Fields Not Properly Being Changed
我有两个 classes,Entity 和 Ship,其中 Ship 扩展了 Entity。在我的实体 class 中,我有一个私有字段 speed。在我的船 class 中,我有一个更新方法,它只是试图通过说当前速度 + 加速度乘以增量时间来增加速度。
这是实体:
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; }
这是 Ship 的 update()
方法:
setSpeed (getSpeed () + acceleration * delta);
System.out.println (getSpeed ());
我希望发生的事情是,无论加速度 * 增量是多少,速度都会增加。但是,发生的情况是速度设置为等于该值,而不是递增。如果我将 Entity 中的字段设置为静态,那么我的代码就可以工作,但我觉得我不应该那样做。我还尝试将字段设置为受保护,将实体 class 设置为抽象,但我仍然得到相同的效果。我真的不知道我做错了什么,除非将字段设置为静态是正确的吗?
编辑:
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 ());
}
}
编辑#2:我做了一些额外的测试,如果我在主要的 render() 方法中放置一个 ship 实例,LibGDX 就可以了。然而,在其他任何地方,它都没有。
我相信我修复了它,或者至少据我所知。显然说 static Ship ship = new Ship ();
允许字段正确更改,而这些字段也不需要是静态的。
我有两个 classes,Entity 和 Ship,其中 Ship 扩展了 Entity。在我的实体 class 中,我有一个私有字段 speed。在我的船 class 中,我有一个更新方法,它只是试图通过说当前速度 + 加速度乘以增量时间来增加速度。
这是实体:
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; }
这是 Ship 的 update()
方法:
setSpeed (getSpeed () + acceleration * delta);
System.out.println (getSpeed ());
我希望发生的事情是,无论加速度 * 增量是多少,速度都会增加。但是,发生的情况是速度设置为等于该值,而不是递增。如果我将 Entity 中的字段设置为静态,那么我的代码就可以工作,但我觉得我不应该那样做。我还尝试将字段设置为受保护,将实体 class 设置为抽象,但我仍然得到相同的效果。我真的不知道我做错了什么,除非将字段设置为静态是正确的吗?
编辑: 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 ());
}
}
编辑#2:我做了一些额外的测试,如果我在主要的 render() 方法中放置一个 ship 实例,LibGDX 就可以了。然而,在其他任何地方,它都没有。
我相信我修复了它,或者至少据我所知。显然说 static Ship ship = new Ship ();
允许字段正确更改,而这些字段也不需要是静态的。