简体   繁体   English

Libgdx / Java继承问题

[英]Libgdx/Java inheritance issue

I'm currently working on an android project using libgdx and java and have run into an issue that I'm not sure where I'm going wrong. 我目前正在使用libgdx和java开发一个android项目,并且遇到了一个不确定我要去哪里的问题。 I have a base class that extends widget. 我有一个扩展小部件的基类。 This base class has a protected member that is set in the act() method of each derived class, but the base statBar class uses the member in its own draw method. 该基类具有一个受保护的成员,该成员在每个派生类的act()方法中设置,但基StatBar类在其自己的draw方法中使用该成员。 Problem is that when the base class method gets called the member is 0 when it should not be. 问题是,当调用基类方法时,成员不应该为0。 What am I missing? 我想念什么?

Base class: 基类:

public abstract class statBar extends Widget {
  protected Color color;
  protected Color darkColor;
  protected Combatant character;

  protected float currentFillWidth;
  private Drawable fullBar;
  privates Drawable emptyBar;

  public statBar(Color color, Combatant character, Skin skin){
    this.character = character;
    this.color = color;
    this.darkColor = color.cpy().mul(0,0,0,.2f);
    this.fullBar = skin.newDrawable("statBar", this.color);
    this.emptyBar = skin.newDrawable("statBar", this.darkColor);
  }

  @Override
  public void draw(Batch batch, float parentAlpha) {
    float x = getX();
    float y = getY();
    float width = getWidth();
    float height = getHeight();

    // draw current fill box
    fullBar.draw(batch, x, y, currentFillWidth, height);

    // if not full, draw empty bar portion
    if (currentFillWidth < width) {
      emptyBar.draw(batch, x+currentFillWidth, y, width-currentFillWidth, height);
    }
  }
}

Derived class: 派生类:

public class HealthBar extends statBar {
  public HealthBar(Combatant character, Skin skin) {
    super(Color.RED, character, skin);
  }

  @Override
  public void act(float delta) {
    super.act(delta);
    currentFillWidth = getWidth() * (character.getCurHP() / character.getMaxHP());
  }
}

Finally this is how I'm instantiating the bar, it's added to a larger GUI table on my screen, but really the rest is irrelevant 最后,这就是我实例化该条的方式,它已添加到屏幕上的一个较大的GUI表中,但实际上其余的都无关紧要

ui.add(new HealthBar(c, skin));

c is the current iteration of character in the player's party. c是玩家聚会中角色的当前迭代。 Everything displays fine when at full hp, but when your not full, it draws the bar empty. 满功率时一切显示正常,但不满功率时,它会将条拉空。 Debugging gives me he fact that when the base class draw method is called, currentFillWidth is once again 0 when it should not be. 调试给我提供了这样一个事实,即当调用基类的draw方法时,currentFillWidth再次为0,而不应为0。 What am I missing about inheritance that a base class method is not getting updated member values from the derived class? 对于基类方法没有从派生类获取更新的成员值的继承,我遗漏了什么? Libgdx calls act on the widget when I call ui.act in my render method before ui.draw. 当我在ui.draw之前的渲染方法中调用ui.act时,Libgdx调用将作用于小部件。 I have even tried overriding draw in the derived class and forcing a super.draw call. 我什至尝试在派生类中重写draw并强制进行super.draw调用。 Same results. 结果相同。

Remove parenthesis from 从中删除括号

currentFillWidth = getWidth() * (character.getCurHP() / character.getMaxHP());

So 所以

currentFillWidth = getWidth() * character.getCurHP() / character.getMaxHP();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM