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

2019/3/7

Can't delete projectile when hitting Bricks

Ni3ls Ni3ls

2019/3/7

#
I have a projectile Class Bullet, which should be deleted when it hits anything. I also have a wall that can't be passed through by any object using setLocation(int x, int y)
public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Bricks.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }
I want to delete the bullet when it "hits" the Bricks, but the Bullet never hits the bricks due to the code above. I have already tried putting this in my Bricks Class, but it didn't work
public class Bricks extends Actor
{
    /**
     * Act - do whatever the Bricks wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        detectBullet();
    }  

    public void detectBullet()
    {
        Actor actor = getOneObjectAtOffset(25, 25, Bullet.class);
        if(actor!=null)
        {
            getWorld().removeObject(actor);
        }
    }
}
And I have also tried putting this in Bullet Class, but this didn't work either
public void detectWall()
    {
        Actor actor = getOneObjectAtOffset(25, 25, Bricks.class);
        if(actor!=null)
        {
            getWorld().removeObject(this);
        }
    }
What code should I use to make the Bullet disappear and in what class should it be written?
danpost danpost

2019/3/7

#
Ni3ls wrote...
I have a projectile Class Bullet, which should be deleted when it hits anything. I also have a wall that can't be passed through by any object using setLocation(int x, int y) << Code Omitted >> I want to delete the bullet when it "hits" the Bricks, but the Bullet never hits the bricks due to the code above.
Then change what the code above does when a Brick object is hit.
I have already tried putting this in my Bricks Class, but it didn't work << Code Omitted >> And I have also tried putting this in Bullet Class, but this didn't work either << Code Omitted >>
(instead of trying to add more code).
What code should I use to make the Bullet disappear and in what class should it be written?
getWorld().removeObject(this);
(in the Bullet class).
You need to login to post a reply.