Hey, I’m trying to setup a class hierarchy to do things quicker. I have an entity class and all of the others will inherit.
Problem is, because the constructor should include superclass’ constructor and it should be the first line in the constructor, I cannot do what I want.
This is my class:
public class Entity {
public float BOX_TO_WORLD;
public float WORLD_TO_BOX;
// Drawing
public Sprite sprite;
public Texture texture;
public SpriteBatch spriteBatch;
// Box2D
public Body body;
public FixtureDef fixtureDef;
public BodyDef bodyDef;
public Shape shape;
public World world;
public Entity(Texture texture, SpriteBatch spriteBatch, World world,
FixtureDef fixtureDef, BodyDef bodyDef, Shape shape, float WORLD_TO_BOX, float BOX_TO_WORLD) {
// this.texture = texture;
this.spriteBatch = spriteBatch;
this.fixtureDef = fixtureDef;
this.bodyDef = bodyDef;
this.shape = shape;
this.world = world;
this.BOX_TO_WORLD = BOX_TO_WORLD;
this.WORLD_TO_BOX = WORLD_TO_BOX;
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
sprite = new Sprite(texture);
//Do other stuff etc.
}
//I've only posted the constructor
So by doing it like this, I want to create a Player class and define fixtureDef, bodyDef, texture and other variables in that class. So that when I want to instantiate the Player, I would only need World and SpriteBatch. Meaning the constructor of my Player class would be like this:
public class Player extends Entity {
Texture texture;
FixtureDef fixtureDef;
BodyDef bodyDef;
CircleShape shape;
public Player(SpriteBatch spriteBatch, World world, float WORLD_TO_BOX, float BOX_TO_WORLD) {
//Planned to define texture, fixtureDef, bodyDef and shape and set their attributes here
super(texture, spriteBatch, world, fixtureDef, bodyDef, shape, WORLD_TO_BOX,
BOX_TO_WORLD);
// TODO Auto-generated constructor stub
}
}
So I would instantiate a Player easily just like this:
Player play = new Player(spriteBatch, world, WORLD_TO_BOX, BOX_TO_WORLD);
But the super constructor should be the first line. Moreover, I cannot use it if I set variables in the Player class which is understandable.
What can I do here?