Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:16:11+00:00 2026-06-16T19:16:11+00:00

I have class which represents every object in my simple game (player, enemy, beam

  • 0

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:

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

I have extended the class Player:

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 < targetX+3) && ((int)x+20 > 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) > abs(distanceY)) {
            shiftY = abs(distanceY) / abs(distanceX) * speed;
        }
        if (abs(distanceY) > abs(distanceX)) {
            shiftX = abs(distanceX) / abs(distanceY) * speed;
        }
        //Zmiana kierunku shifta w zaleznosci od polozenia
        if (distanceX < 0) {
            shiftX = -shiftX;
        }
        if (distanceY < 0) {
            shiftY = -shiftY;
        }
        //Jezeli statek mialby wyjsc poza granice to przerywamy
        if ( (((int)x+shiftX < 0) || ((int)x+shiftX > 260)) || ((y+shiftY < 0) || (y+shiftY > 360)) ) {
            return;
        }
        //Zmiana pozycji gracza
        x += shiftX;
        y += shiftY;
    }
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:16:12+00:00Added an answer on June 16, 2026 at 7:16 pm

    You need to use getX(), getY() etc., because x,y, speed are private variables for class Thing.

    The fact that Player extends Thing doesn’t mean Player can access private fields. Thing provided public get... set... to access its private variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which represents exam,it can start/reset/pause exam, so the methods look
Say I have a class which represents a person, a variable within that class
I have a Location class which represents a location somewhere in a stream. (The
I have a class, called Media, which represents coredata. If something changes in my
I have class World which manages creation of object... After creation it calls afterCreation
I have a generic class which represents a fragment of text. That fragment of
Assuming I have a simple class that represents a staff member class Staff {
I am writing a basic game engine and have an abstract class that represents
I am creating an object server-side which is to have all of the data
I have class which have one public method Start , one private method and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.