1

so I have an object, and I want to make it so that whenever a projectile collides with that object, it gradually switches its sprites, and finally disappear. How can I do that? Here are my codes (sprites is a list of sprites that I need to set, and fireballs is a list of fireballs) Firstly, the chunk of code that I want to fix:

for(int i = 0; i < this.fireballs.size(); i++){
        if(this.brickwall.wallExist()){
            if((fireballs.get(i).getX() <= this.brickwall.getX()+10 & fireballs.get(i).getX() >= this.brickwall.getX()-10) && 
            (fireballs.get(i).getY() <= this.brickwall.getY()+10 & fireballs.get(i).getY() >= this.brickwall.getY()-10)){
                fireballs.remove(i);
                int counter = 0;
                for(PImage p : sprites){
                    if(counter == 4){
                        this.brickwall.setSprite(p);
                        this.brickwall.draw(this);
                        counter = 0;
                    }
                    else{
                        counter++;
                    }
                }          
            }
    }
}

fireball is an extension of the class projectile:

public abstract class Projectile {
    protected int x;
    protected int y;
    protected PImage sprite;

    public Projectile(int x, int y){
        this.x = x;
        this.y = y;
    }
    public void draw(PApplet app) {
        // The image() method is used to draw PImages onto the screen.
        // The first argument is the image, the second and third arguments are coordinates
        app.image(this.sprite, this.x, this.y);
    }
    //set sprite
    public void setSprite(PImage sprite) {
        this.sprite = sprite;
    }
    public abstract void Up();
    public abstract void Down();
    public abstract void Left();
    public abstract void Right();
    public int getX(){
        return this.x;
    }
    public int getY(){
        return this.y;
    }
}

public class Fireball extends Projectile {
    public char dir = 'n';
    public Fireball(int x, int y) {
        super(x, y);
    }

    public void Right() {
        this.x += 4;
    }
    public void Left(){
        this.x -= 4;
    }
    public void Up() {
        this.y -= 4;
    }
    public void Down() {
        this.y += 4;
    }
        
}
    

brickwall is an extension to the class wall:

import processing.core.PImage;
import processing.core.PApplet;

public abstract class wall extends PApplet {
    protected int x;
    protected int y;
    protected PImage sprite;
    protected int size;
    public wall(int x, int y){
        this.x = x;
        this.y = y;
    }
    public void setSprite(PImage sprite) {
        this.sprite = sprite;
    }
    public void draw(PApplet app){
        app.image(this.sprite, this.x, this.y);
    }
    public int getX(){
        return this.x;
    }
    public int getY(){
        return this.y;
    }
    public void tick(){
        
    }
}
import processing.core.PImage;

public class brickwall extends wall {
    public PImage sprite;
    public int counter = 0;
    protected PImage[] destroyed = new PImage[4];

    public brickwall(int x, int y){
        super(x, y);
    }
    public boolean wallExist(){
        return true;
    }

    public void destroy(){
        //erase the sprite
        this.sprite = null;
    }
    public void tick(){
        if(this.counter < 4){
            this.sprite = destroyed[this.counter];
            this.counter++;
        }
    }
}

0 Answers0