I apologize if this is in the wrong board. The moderators can move it if it is.
I have two abstract classes. Rocket and Projectile, which look like this (I have only left the relevant parts)
public abstract class Rocket extends Entity{
protected int dx, dy;
protected int y;
protected int width = 20, height = 20;
protected BufferedImage rocket = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
public Rocket(int x, int y, int health, int dir, int speed) {
super(x, y, health, dir);
this.y = y;
this.dy = speed;
this.dx = speed;
visible = false;
}
public void tick(){
y = y + dy;
if(y > Game.WIDTH * Game.SCALE){
visible = true;
}
}
public void render(Graphics g){
g.drawImage(rocket, x, y, null);
g.fillRect(x, y, width, height); // temporary
}
public Rectangle bounds() {
return new Rectangle(x, y, width, height);
}
}
public abstract class Projectile extends Entity{
protected int xOrigin, yOrigin;
protected double angle;
protected BufferedImage projectile;
protected double nx, ny;
protected int x, y;
protected double speed, range, damage;
protected boolean removed = false;
public static Rectangle bounds;
public Projectile(int x, int y, int health, double dir) {
super(x, y, health, dir);
xOrigin = x;
yOrigin = y;
angle = dir;
this.x = x;
this.y = y;
}
protected void move(){
}
public boolean isRemoved(){
return removed;
}
public Rectangle bounds() {
return new Rectangle((int)x, (int)y, width, height);
}
}
Then I have subclasses extending those two classes (I will show NormalRocket and MachineGunProjectile) which call the super of bounds like so:
public class NormalRocket extends Rocket{
public int x;
public int y;
int dx, dy;
public int width = 20, height = 20;
int burnDuration = 20;
int timer = 0;
Base base = new Base(0, 0, 0, 0, 0, 0);
public NormalRocket(int x, int y, int health, int dir, int speed) {
super(x, y, health, dir, speed);
this.x = x;
this.y = y;
visible = false;
try {
rocket = ImageIO.read(new File("res/rocket_normal.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void tick(){
super.tick();
}
public void render(Graphics g){
super.render(g);
}
public Rectangle bounds(){
return super.bounds();
}
public boolean getVisible(){
return visible;
}
}
public class MachineGunProjectile extends Projectile{
private int width = 6, height = 6;
private int x, y;
public static final int FIRE_RATE = 15;
public MachineGunProjectile(int x, int y, int health, double dir){
super(x, y, health, dir);
this.x = x;
this.y = y;
range = 300;
speed = 4;
damage = 10;
nx = speed * Math.cos(angle);
ny = speed * Math.sin(angle);
bounds = new Rectangle(this.x, this.y, this.width, this.height);
try {
projectile = ImageIO.read(new File("res//MachineGunProjectile.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void tick(){
move();
if(x > Game.WIDTH * Game.SCALE || x < 0 || y > Game.HEIGHT * Game.SCALE || y < 0){
removed = true;
}
}
protected void move(){
x += nx;
y += ny;
}
public boolean isRemoved(){
return super.isRemoved();
}
public Rectangle bounds(){
return super.bounds();
}
public void collision(Rocket rocket){
super.collision(rocket);
}
public void render(Graphics g){
g.drawImage(projectile, (int)x, (int)y, null);
}
}
I have a level class which contains two lists, one for rockets and the other for projectiles, and rockets and projectiles are added to the respective lists:
public static ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
private static ArrayList<Rocket> rockets = new ArrayList<Rocket>();
private void spawnRocket(int rocketType, int x) {
switch (rocketType) {
case ROCKET_NORMAL:
rockets.add(new NormalRocket(x, -10, 80, 0, 2));
rocketsSpawned++;
rocketsOnScreen++;
break;
case ROCKET_FIRE:
if(difficulty > 1 && random.nextInt(100) > fireRocketSpawn){
rockets.add(new FireRocket(x, -10, 70, 0, 2));
rocketsSpawned++;
rocketsOnScreen++;
}else{
return;
}
break;
case ROCKET_ZIPPER:
if(difficulty > 2 && random.nextInt(100) > zipperRocketSpawn){
rockets.add(new ZipperRocket(x, -10, 40, 0, 4));
rocketsSpawned++;
rocketsOnScreen++;
}else{
return;
}
break;
case ROCKET_TANK:
if(difficulty > 3 && random.nextInt(100) > tankRocketSpawn){
rockets.add(new TankRocket(x, -10, 130, 0, 1));
rocketsSpawned++;
rocketsOnScreen++;
}else{
return;
}
break;
}
}
I am using the following code to check for collision between the projectiles and rockets. The problem is, that "HIT" is not printed and I cannot figure out why:
private void collision(){
for(int i = 0; i < projectiles.size(); i++){
for(int j = 0; j < rockets.size(); j++){
if(projectiles.get(i).bounds().intersects(rockets.get(j).bounds())){
System.out.println("HIT!");
}
}
}
}
I do not know why this does not work, I have tried 2 different techniques so far and neither work. Can anyone help me solve this problem? I can post more code or an image if it is necessary.

New Topic/Question
Reply


MultiQuote





|