kirchhoff
kirchhoff

Reputation: 216

Creating a class for my game (Slick)

I'm making my first game in Java with Slick2d and I created a class plane. (i'm new with java too)

I'm not sure if it's correct to extend from Image class in my class or I'd better use a public Image attribute in my class (public to have easy access to it's methods in the rest of the proyect)

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Plane extends Image{
private float x;
private float y;

public Plane(String path, float x, float y) throws SlickException{
    super(path);
    this.x = x;
    this.y = y;

}

    public float getX(){
    return x;
}

public float getY(){
    return y;
}

public void move(float hip){             
    float rotation = this.getRotation();
    x+= hip * Math.sin(Math.toRadians(rotation));
    y-= hip * Math.cos(Math.toRadians(rotation));
}

}

Upvotes: 1

Views: 490

Answers (2)

michael
michael

Reputation: 667

Well, if your class extends Image, then technically your class would inherit all of the properties of the Image class and in theory become an Image.

From your code, I suggest you either:

  1. Instead of extending Image, just create an Image variable in your class.
  2. Extend Image, then make another class that uses this Image.

Upvotes: 0

Thomas
Thomas

Reputation: 88757

I don't know slick2d but I'd say your class should not extend Image but have an image attribute. Extending is a way of saying "Plane is an Image" which normally is not the case but rather a plane has a texture/image.

Additionally, the image should not be public but protected/private and be accessed by public getters and setters. That way you increase encapsulation and prevent problems that might arise when making the image itself public.

Upvotes: 5

Related Questions