Reputation: 55
I am quite new to inheritance and all that fun stuff. So I have an enemy class that breaks out into a bunch of different types of enemies. This last enemy I am doing will have a different tick() then the rest so I need to override. However I dont know how to override because then it can't use all the variables that are in the enemy class. The option it gives me is to make my get methods static in my GameObject abstract class -> Enemy -> Smart Enemy. But I am not new to this and I feel like that could cause issues?
package first.Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Enemy extends GameObject{
private Handler handler;
private Color c;
private int eWidth, eHeight, speedX, speedY, x, y;
public Enemy(int x, int y, int eWidth, int eHeight, int speedX, int speedY, Color c, ID id, Handler handler)
{
super(x, y, id);
this.x = x;
this.y = y;
this.id = ID.Enemy;
this.c = c;
this.eWidth = eWidth;
this.eHeight = eHeight;
this.handler = handler;
this.speedX = speedX;
this.speedY = speedY;
}
public Rectangle getBounds()
{
return new Rectangle(x, y, eWidth, eHeight);
}
public void tick()
{
x += speedX;
y += speedY;
if(y <= 0 || y >= Game.HEIGHT - 48) speedY *= -1; //What can I do instead of 48?
if(x <= 0 || x >= Game.WIDTH - 32) speedX *= -1;
handler.addObject(new Trail(x, y, ID.Trail, c, eWidth, eHeight, 0.03f, handler));
}
public void render(Graphics g)
{
g.setColor(c);
g.fillRect(x, y, eWidth, eHeight);
}
}
package first.Game;
import java.awt.Color;
public class SmartEnemy extends Enemy{
private GameObject player;
//private Handler handler;
//private int x, y, speedX, speedY, eWidth, eHeight;
//private Color c;
public SmartEnemy(int x, int y, Handler handler)
{
super(x, y, 16, 16, 0, 0, Color.green, ID.Enemy, handler);
//this.handler = handler;
//this.x = x;
//this.y = y;
for(int i = 0; i < handler.object.size(); i++)
{
if(handler.object.get(i).getId() == ID.Player)
player = handler.object.get(i);
}
}
@Override
public void tick()
{
float diffX = x - player.getX(); //- 8
float diffY = y - player.getY(); //- 8
float distance = (float) Math.sqrt((x - player.getX()) * (x - player.getX()) + (y - player.getY()) * (y - player.getY()));
speedX = (int) ((-1.0 / distance) * diffX);
speedY = (int) ((-1.0 / distance) * diffY);
if(y <= 0 || y >= Game.HEIGHT - 48) speedY *= -1; //What can I do instead of 48?
if(x <= 0 || x >= Game.WIDTH - 32) speedX *= -1;
handler.addObject(new Trail(x, y, ID.Trail, c, eWidth, eHeight, 0.03f, handler));
}
}
Upvotes: 1
Views: 50
Reputation: 1678
You need some help from protected
public class Enemy extends GameObject{
protected Handler handler;
protected Color c;
protected int eWidth, eHeight, speedX, speedY, x, y;
}
You can access Enemy variables from children classes now.
Upvotes: 1
Reputation: 393781
If you wish to access instance variables of Enemy
from SmartEnemy
, you have several options:
If you only need read only access, add to Enemy
class a getter method that returns the value of the required Enemy
instance variable. If only classes in the Enemy
hierarchy should have access to the value of that instance variable, give it a protected
access modifier.
If you also need write access, either add a setter method with protected
access modifier, or give the instance variable itself a protected
access modifier.
Upvotes: 1