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

2023/5/2

How to make a bullet disappear after hitting?

Wizzzarium Wizzzarium

2023/5/2

#
Im trying to have my bullet removed after it hits one "Öl". If I just use removeObject(this) in the same code in which I remove the Öl it gives me a red error window.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends Animal
{
    /**
     * Act - do whatever the Projectile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    Garten thisGame; 
    public void act()
    {
         move();
         
         hit1();
         //hit2();
         Wall();
         //despawn();
    }
    public void move()
    {
        move(10);
    }
    public void hit1()
    {
        Actor Projectile = getOneIntersectingObject(Öl.class); //Kein plan wie die 2 zeilen funktionieren
        if (Projectile != null) {
            getWorld().removeObject(Projectile);
            
            thisGame.score++;
        }
    }
    public void hit2()
    {
        Actor Öl = getOneIntersectingObject(Projectile.class);
        if (Öl != null) {
            getWorld().removeObject(Öl);
            
        }
    }
    public void despawn()
    {if(isTouching(Öl.class));
        getWorld().removeObject(this);

    }

    public void Wall()
    {
        Actor Projectile_Wall = getOneIntersectingObject(Projectile.class); //Kein plan wie die 2 zeilen funktionieren
        if (Projectile_Wall != null) {
            getWorld().removeObject(Projectile_Wall);
    }
}
}
danpost danpost

2023/5/2

#
Wizzzarium wrote...
Im trying to have my bullet removed after it hits one "Öl". If I just use removeObject(this) in the same code in which I remove the Öl it gives me a red error window. << Code Omitted >>
After every method called by act whose code can remove this actor, you should have the following:
if (getWorld() == null) {
    return;
}
(so that no further code requiring the actor be in the world will not cause an error). With the code as given, it would not be necessary as there is no code following the call to despawn in act, which is the only method, at present, that removes this actor. However, if you add a removal line in the hit2 method to remove this actor, you will need that code following line 21 (if line 21 was not commented).
Wizzzarium Wizzzarium

2023/5/9

#
thank you
Wizzzarium Wizzzarium

2023/5/9

#
<3
You need to login to post a reply.