Note that there are some explanatory texts on larger screens.

plurals
  1. POPrivate fields from abstract class cannot be accessed in the subclass
    primarykey
    data
    text
    <p>I have class which represents every object in my simple game (player, enemy, beam etc - they all have many commons like speed, position, dmg). So i made class named Thing. Here is how it looks like:</p> <pre><code>public abstract class Thing { private Image image; private float x; private float y; private float speed; private final int WIDTH; private final int HEIGHT; public Thing(String filename, float x, float y, float speed) { try { Image image = ImageIO.read(new File(filename)); } catch (Exception e) {} this.x = x; this.y = y; this.speed = speed; WIDTH = image.getWidth(null); HEIGHT = image.getHeight(null); } //Zwraca ksztalt do sprawdzania czy contains... public Rectangle2D getShade() { return new Rectangle2D.Float(x, y, WIDTH, HEIGHT); } public Image getImage() { return image; } public Point2D getPoint() { return new Point2D.Float(x, y); } public float getX() { return x; } public float getY() { return y; } } </code></pre> <p>I have extended the class Player:</p> <pre><code>public class Player extends Thing { public Player(String filename, float x, float y, float speed) { super(filename, x, y, speed); } public void moveToPoint(Point2D targetPoint) { int targetX = (int)targetPoint.getX(); int targetY = (int)targetPoint.getY(); if ( ((int)x+20 &lt; targetX+3) &amp;&amp; ((int)x+20 &gt; targetX-3) ) { return; } float distanceX = targetX - x; float distanceY = targetY - y; //Dodanie 20px wymiarow statku distanceX -= 20; distanceY -= 20; //Ustalenie wartosci shiftow float shiftX = speed; float shiftY = speed; if (abs(distanceX) &gt; abs(distanceY)) { shiftY = abs(distanceY) / abs(distanceX) * speed; } if (abs(distanceY) &gt; abs(distanceX)) { shiftX = abs(distanceX) / abs(distanceY) * speed; } //Zmiana kierunku shifta w zaleznosci od polozenia if (distanceX &lt; 0) { shiftX = -shiftX; } if (distanceY &lt; 0) { shiftY = -shiftY; } //Jezeli statek mialby wyjsc poza granice to przerywamy if ( (((int)x+shiftX &lt; 0) || ((int)x+shiftX &gt; 260)) || ((y+shiftY &lt; 0) || (y+shiftY &gt; 360)) ) { return; } //Zmiana pozycji gracza x += shiftX; y += shiftY; } } </code></pre> <p>And here is the problem because my IDE underlines x, y and speed fields red and tells they cannot be accessed from Player class. I tried to change them into private and default but there appears an error after that. What am I doing wrong? When i create new object from class which extends Thing I want to copy all fields and init them as it is said in constructor. So how to repair it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload