#Help with super class and sub class optimization
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Detected code, here are some useful tools:
public class Tower {
// image that represents the gear's position on the board
protected BufferedImage image;
protected Image newImage;
// current position of the gear on the board grid
protected Point pos;
protected String imageFile;
protected void loadImage() {
try {
// you can use just the filename if the image file is in your
// project folder, otherwise you need to provide the file path.
image = ImageIO.read(new File(imageFile));
newImage = image.getScaledInstance(Board.TILE_SIZE, Board.TILE_SIZE, Image.SCALE_DEFAULT);
} catch (IOException exc) {
System.out.println("Error opening image file: " + exc.getMessage());
}
}
public void draw(Graphics g, ImageObserver observer) {
// with the Point class, note that pos.getX() returns a double, but
// pos.x reliably returns an int. https://stackoverflow.com/a/30220114/4655368
// this is also where we translate board grid position into a canvas pixel
// position by multiplying by the tile size.
g.drawImage(newImage, pos.x * Board.TILE_SIZE, pos.y * Board.TILE_SIZE, observer);
}
public Point getPos() {
return pos;
}
public void tick() {
// called before repaint
}
}
create a constructor in tower class, which takes a Point gpos, then in each subclass' constructor, call super(gpos);
oh, also, it would take a string
for the image file
protected Tower(Point gpos, String imageFile) {
// current position of the gear on the board grid
this.pos = gpos;
this.imageFile = imageFile;
// load the assets
loadImage();
// initialize the state
pos = gpos;
}
example in your canon class
class Cannon extends Tower {
public Cannon(Point gpos) {
super(gpos, "src/images/scratch_cat.png");
}
}
also, private the loadImage method, since it's responsibility of the Tower class to initialise properly the object, not the subclass (since it's protected)
also, be careful with inheritance, it sometimes can "slow" the process, since you need to care about the hierarchy of the thing, prefer (not now, but in future projects) composition (use fields) over inheritance
in some projects inheritance is fine, but in games... it can be hard when it becomes more and more complex
there's a lot of things going on, so I'm not sure exactly how we could refactor this, I'll see your github and i'll try to suggest something
So first, why dont you have abstract methods ?
Also, avoid inheritance, and you shouldn't use regular class as a base type, that's the goal of an interface
A better way to solve your problem is simply to not extend tower, and instead create a TowerFunction interface that will be implemented by every possible tower ability, then you assign a field of TowerFunction in Tower
@ashen marlin