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

2016/10/9

My bombs are duds

Betty77 Betty77

2016/10/9

#
So I'm writing a little game where you fly around in a biplane and drop bombs on bad guys, in this case turrets. However I've run into a little problem. At first, before I wrote the code to remove the bomb on impact, it would go right through the turret, instantly killing it (I want it to take a couple hits, and while passing through the target of course it's hitting multiple times) That's nothing I'm not familiar with. The problem is this, now that I have written code to remove the bomb on impact, apparently it's removing it prior to the health of the turret getting deducted, so I can drop bombs on the turret all day long, and nothing happens to it. Here's the bomb code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bomb extends Actor
{
    /**
     * Act - do whatever the bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);

        if(isTouching(turret.class))
        {
            World myWorld = (getWorld());
        myWorld.removeObject(this);
        }
    }
}
And here's the turret code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class turret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class turret extends Actor
{
    private int health = 2;
    /**
     * Act - do whatever the zeppelin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        Actor bomb=getOneIntersectingObject(bomb.class);
        {
            if (bomb !=null)
            {
                health--;
            }
            {
                if (health==0)
                    die();
            }

        }
      
    }

    public void die()
    { 
        World myWorld = (getWorld());
        myWorld.removeObject(this);

    }

}
danpost danpost

2016/10/9

#
The problem is that you have kept separate the codes for touching/removing and decreasing the health (the bombs are acting first and removing themselves before the turret acts and checks for the bombs -- which are not found because they remove themselves). Also, your bombs will remain in the world and pile up at the edge of the world because those that do not end up touching a turret are not being removed from the world. Have bombs remove themselves at edge of world and have turrrets remove any bombs that touch them (you already get a reference to them at line 18).
Betty77 Betty77

2016/10/12

#
Oh, I know how to fix the bomb pile. I just hadn't gotten around to it yet because I was first fixing more pressing problems. Thanks for the advice though. Any suggestions for how my code should look? I'm thinking it might work better to call for the target health reduction in the bomb's code (seems like it would be a little simpler that way) but I'm not 100% sure how I would do that.
danpost danpost

2016/10/12

#
Betty77 wrote...
I'm thinking it might work better to call for the target health reduction in the bomb's code (seems like it would be a little simpler that way) but I'm not 100% sure how I would do that.
Actually, no; it would be much simpler to remove lines 19 through 23 from the bomb class and just insert the following to the turret class at line 22:
getWorld().removeObject(bomb);
That is what was meant when:
danpost wrote...
have turrrets remove any bombs that touch them (you already get a reference to them at line 18).
This combines the two parts, touching/removing and deceasing the health.
You need to login to post a reply.