Java - 超类字段未正确更改

时间:2016-04-08 20:53:32

标签: java libgdx subclass superclass

我有两个类,实体和船舶,船舶扩展实体。在我的实体类中,我有一个私人场速。在我的船类中,我有一个更新方法,它只是试图通过说当前速度+加速乘以增量时间来提高速度。

这是实体:

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 ());

我期望发生的是,无论加速度*是多少,速度都会增加。然而,发生的是速度被设置为等于该值,而不是递增。如果我将实体中的字段设置为静态,那么我的代码可以工作,但我觉得我不应该这样做。我也尝试将字段设置为protected并将Entity类设置为abstract,但我仍然可以获得相同的效果。我真的不知道我做错了什么,除非将字段设置为静态是正确的吗?

编辑: 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:我做了一些额外的测试,如果我把一个船只实例放在LibGDX赋予它的主render()方法中。然而,在其他任何地方,它都没有。

1 个答案:

答案 0 :(得分:0)

我相信我已经修好了,或者至少我已经知道了。显然说static Ship ship = new Ship ();允许字段正确更改,而这些字段也不需要是静态的。