【问题标题】:Java - Superclass Fields Not Properly Being ChangedJava - 未正确更改超类字段
【发布时间】:2016-04-08 20:53:32
【问题描述】:

我有两个类,Entity 和 Ship,其中 Ship 扩展了 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; }

这里是 Ship 的 update() 方法:

setSpeed (getSpeed () + acceleration * delta);
System.out.println (getSpeed ());

我期望发生的是,无论加速度 * delta 是多少,速度都会增加。但是,发生的情况是速度设置为等于该值,而不是增加。如果我将实体中的字段设置为静态,那么我的代码就可以工作,但我觉得我不应该那样做。我还尝试将字段设置为受保护,并将实体类设置为抽象,但我仍然得到相同的效果。我真的不知道我做错了什么,除非将字段设置为静态是正确的?

编辑: 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() 方法中放置一个船实例,它就可以工作。然而,在其他任何地方,它都没有。

【问题讨论】:

  • 加速度或增量为零?回忆一下 Java 的优先规则。
  • 不,增量几乎为零,但不完全为零。
  • 它们是什么类型的?浮动?两个?
  • 嗯,想不出这个。而且看不出为什么静态更好。
  • 好的,请贴出完整的方法。

标签: java libgdx subclass superclass


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多