2016-04-08 32 views
2

İki sınıf var, Varlığın ve Geminin Varlığı genişlettiği yer var. Benim varlık sınıfımda özel bir alan hızım var. Gemi sınıfımda, sadece hız + hızlanma sürelerini bir delta saati olarak söyleyerek hızını artırmaya çalışan bir güncelleme yöntemim var. İşte Java - Süper Sınıf Alanları Düzgün Olarak Değiştirilmiyor

Varlık geçerli:

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; } 

Ve burada Gemi update() yöntemdir:

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

Ne gerçekleşmesini beklediklerini hız * delta ne olursa olsun hızlanma artar olduğunu. Ancak, ne olur, bu hız artırılmış değil, o değere eşit olarak ayarlanır. Varlıktaki alanları statik olarak ayarlarsanız kodum çalışır, ancak bunu yapmamam gerektiğini hissediyorum. Ayrıca alanları koruma altına almayı ve Entity sınıfını soyutlamayı denedim ama yine de aynı etkiyi elde ediyorum. Alanları yanlış ayarlamamak için yanlış yaptığım şey konusunda gerçekten bir kayıp yaşıyorum?

DÜZENLEME: Geminin tüm güncelleştirme yöntemi.

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

DÜZENLEME # 2: Bazı ekstra test yaptım ve ana render() yönteminde bir gemi örneği koyarsanız LibGDX çalıştığını verir. Ancak başka herhangi bir yerde değil.

+0

Hızlanma mı yoksa delta mı, sıfır mı? Java'nın önceliklerini hatırla. – Mordechai

+0

Hayır, delta neredeyse sıfırdır, ancak sıfır değildir. –

+0

Bunlar ne tür? yüzer? Bunların her ikisi de? – Mordechai

cevap

0

Sanırım, ya da en azından anlatabildiğim kadarıyla tamir ettim. Görünüşe göre static Ship ship = new Ship();, alanların da statik olmak zorunda kalmadan alanları düzgün bir şekilde değiştirmesine izin veriyor.

İlgili konular