This site requires JavaScript, please enable it in your browser!
Greenfoot back
Milka
Milka wrote ...

2021/1/23

remove bullet when reaching edge - MVC

Milka Milka

2021/1/23

#
hello! For this game i need to remove the bullets when they reach the end. now they just stop at the edge. I work with the model-view-controller method. This is the Bullet class: (Kogel = bullet)
public class Kogel {

    private double x;
    private double y;
    private String richting;
    //private boolean kogelInVeld;

    private static final int SNELHEID = 5;
    private static final int BREEDTE = 30; //breedte van de Shooter

    /**
     * @param x de x-coordinaat van de kogel
     * @param y de y coordinaat van de kogel
     */
    public Kogel(double x, double y, String richting) {

        this.richting = richting;

        switch (richting) {     
            case "l":
                this.y = y + BREEDTE / 2; 
                this.x = x;
                break;
            case "r":
                this.x = x + BREEDTE;
                this.y = y + BREEDTE / 2;
                break;
            case "u":
                this.x = x + BREEDTE / 2;
                this.y = y;
                break;
            default:
                this.x = x + BREEDTE / 2;
                this.y = y + BREEDTE;
                break;

        }
    }

    /**
     *
     * @return the x
     */
    public double getX() {
        return x;
    }

    /**
     * @return the y
     */
    public double getY() {
        return y;
    }

    public boolean checkKogelInVeld() {
        return (this.x <= 940 && this.x >= 10 && this.y <= 735 && this.y >= 10); 

    }

    // move bullet
    public void tick() {
        if (checkKogelInVeld()) {
            switch (richting) {
                case "l":                 
                    this.x -= SNELHEID;
                    break;
                case "r":
                    this.x += SNELHEID;
                    break;
                case "u":
                    this.y -= SNELHEID;
                    break;
                case "d":
                    this.y += SNELHEID;
                    break;
            }
        } else {
            // TODO: remove bullet

        }

    }

}
The controller Class: The bulletView Class:
    private Kogel model;

    public static final int STRAAL = 5;
    
    public KogelView(Kogel model) {
        this.model = model;
        update();
    }

    public void update() {
        getChildren().clear();
        Circle kogel = new Circle (model.getX(), model.getY(), STRAAL);
        kogel.setFill(Color.RED);
        getChildren().add(kogel);
                        
    }
    
Roshan123 Roshan123

2021/1/23

#
if(isAtEdge())
getWorld().removeObject(this);
It may work or wait for the danpost(one of the most respected user of greenfoot) to reply
MrCohen MrCohen

2021/1/23

#
The Controller class is not posted ... but just looking at this ... There are easy methods for this if your object was an Actor, but for some reason, you've chosen not to make your objects as Actors. I've done quite a bit of Greenfoot coding, and my opinion, there are very few scenarios where it makes sense to do this for things that will get drawn, whether you want to use simple methods like isAtEdge() or code your own similar method.
danpost danpost

2021/1/23

#
In KogelView class update method, you would need to check with:
if ( ! model.checkKogelInVeld() )
and then remove the KogelView object from its controller to effectively remove the bullet it controls.
You need to login to post a reply.