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

2015/9/21

Prevent a class from performing an action

Collin Collin

2015/9/21

#
Hi! I have a Lobster which shoots bombs at Alligators when the space bar is pressed. This all works fine. Now I want the Lobster to have a certain amount of ammunition. When this ammunition runs out, I want the Lobster not to be able to shoot when the space bar is pressed. Please help??
danpost danpost

2015/9/22

#
Just declare an 'int' field to either track the number of shots fired or to track the number of shots remaining to be fired. Alter its value each time a shot is fire and restrict firing to when its value allows it.
Collin Collin

2015/9/22

#
I have everything except for the restrict firing part. How do I tell the Lobster it is unable to shoot, when the ammo integer is 0. I tried this:
if(Greenfoot.isKeyDown("space"))
        {
            World myWorld= getWorld();
            Water water = (Water)myWorld;
            AmmoBarL ammobarL = water.getAmmoBarL();
            if(ammobarL.ammo >=0)
            {
                fire();
                ammobarL.loseAmmo();
            }

            if(ammobarL.ammo <=0)
            {
                !fire();
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(100);
                water.removeObject(errormessage);
            }
        }
but it tells me that !fire(); is not a statement (which is kind of understandable :D )
danpost danpost

2015/9/22

#
You should understand that the computer will not do anything unless it is told to. You do not have to specify that you do not want it to 'fire' -- just do not mention it. Remove line 14 above.
Collin Collin

2015/9/22

#
No, that doesn't work. The Error Message pops up, then vanishes and the Lobster fires a bullet. Also when I press the space bar for a longer time, the ammo bar gets a lot smaller even though only one shot is fired. How can I specify that one time pressed (time shouldn't be relevant, so 1 second pressed delivers the same result as 10 seconds pressed) equals one bullet fired and one loss of ammo. I'll give you my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game
 * they do...)
 * 
 * Version: 1
 * 
 * In this version, the lobster walks around randomly. If it runs into a crab it eats
 * it.
 */

public class Lobster extends OverClass
{
    private int gunReloadTime; 
    private int fishDelayCount;
    private int reloadDelayCount;           
    private Vector acceleration;          
    private int shotsFired;
    Vector vector = new Vector();
    
    public Lobster()
    {
        gunReloadTime = 50;
        reloadDelayCount = 0;
        acceleration = new Vector(0, 0.3);    
        shotsFired = 0;
    }
    
    public void act()
    {    
        checkKeypress();
        move(5);
        lookForFish();      
        reloadDelayCount++;
    }
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    public int getShotsFired()
    {
        return shotsFired;
    }
    
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move(20);
            shotsFired++;
            reloadDelayCount = 0; 
        }
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public  boolean  isAtEdge()
    {
        if (getX()>= getWorld().getWidth()-1 || getX() <=0 || 
        getY()>= getWorld().getHeight()-1 ||   getY()<=0)
        {
            return true;
        }
        else {
            return false;
        }
    }
 
        
    /**
     * Randomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
                World myWorld= getWorld();
                Water water = (Water)myWorld;
                SpeedBar speedbar= water.getSpeedBar();              
                if(speedbar.speed >=0)
                {
                    move(3);
                    speedbar.loseSpeed(); 
                }
                
                if(speedbar.speed <=0)
                {
                    move(0);
                }
        }
         if (Greenfoot.isKeyDown("left")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(5);
        }
        if(Greenfoot.isKeyDown("space"))
        {
            World myWorld= getWorld();
            Water water = (Water)myWorld;
            AmmoBarL ammobarL = water.getAmmoBarL();
            if(ammobarL.ammo >=0)
            {
                fire();
                ammobarL.loseAmmo();
            }

            if(ammobarL.ammo <=0)
            {               
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(100);
                water.removeObject(errormessage);
            }
        }
    }
        public void checkKeypress2()
    {
        if (Greenfoot.isKeyDown("up")) 
         {
            
            World myWorld= getWorld();
            Water water = (Water)myWorld;
            SpeedBar speedbar= water.getSpeedBar();
            move(3);
            speedbar.loseSpeed();          
            if(speedbar.speed ==0)
            {
                move(1);
            }
         }
         if (Greenfoot.isKeyDown("left")) 
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(5);
        }
    }
    
    /**
     * Try to pinch a fish. That is: check whether we have stumbled upon a fish.
     * If we have, remove the fish from the game, and stop the program running.
     */
    public void lookForFish()
    {
        
        if ( isTouching(Fish.class) ) 
        {
            World myWorld= getWorld();
            removeTouching(Fish.class);
            Water water = (Water)myWorld;
            SpeedBar speedbar= water.getSpeedBar();
            speedbar.addSpeed();
            ScoreBoard scoreboard= water.getScoreBoard();
            AmmoBarL ammobarL = water.getAmmoBarL();
            scoreboard.addScore();
            ammobarL.addAmmo();
            Greenfoot.playSound("slurp.wav");
            VictoryL victoryL = new VictoryL();
            if (scoreboard.score >=15)
            {
                scoreboard.addScore();
                myWorld.addObject(victoryL, myWorld.getWidth()/2, myWorld.getHeight()/2);
                Greenfoot.delay(150);
                Greenfoot.stop();
                Greenfoot.setWorld(new Water());
            }
            getWorld().addObject(new Fish(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        }
    }
}
davmac davmac

2015/9/22

#
You should also introduce an 'else' to prevent both the 'if' clauses being true. And, probably, your condition for being able to fire is wrong - it should be 'ammobarL.ammo > 0', not 'ammobarL.ammo >= 0'. So:
            if(ammobarL.ammo > 0)
            {
                fire();
                ammobarL.loseAmmo();
            }
            else /* no longer needed: if(ammobarL.ammo <=0) */
            {
                /* !fire(); */
                ErrorMessage errormessage = new ErrorMessage();
                myWorld.addObject(errormessage, 500, 300);
                Greenfoot.delay(100);
                water.removeObject(errormessage);
            }
Collin Collin

2015/9/22

#
Wow great thanks, that workes awesome!! Now how would I do that one key press eqals one shot?
Super_Hippo Super_Hippo

2015/9/22

#
Have a boolean, let's say 'spaceDown'. If it is 'false' and space is pressed, set it to 'true' and shoot. If it is 'true' and space isn't pressed, set it back to 'false'.
danpost danpost

2015/9/22

#
Collin wrote...
Now how would I do that one key press eqals one shot?
You could introduce a boolean field to track the last state of the "space" key. Then use the following:
// the instance field
private boolean spaceDown;
// the code to use
if (Greenfoot.isKeyDown("space") != spaceDown) // if change in state of key
{
    spaceDown = ! spaceDown; // new state of key
    if (spaceDown) // key was pressed
    {
        AmmoBarL ammobarL = ((Water)getWorld()).getAmmoBarL();
        if (ammobarL.ammo > 0)
        {
            fire();
            ammobarL.loseAmmo();
        }
        else
        {
            ErrorMessage errormessage = new ErrorMessage();
            getWorld().addObject(errormessage, 500, 300);
            Greenfoot.delay(100);
            getWorld().removeObject(errormessage);;
        }
    }
}
Collin Collin

2015/9/22

#
Thanks a lot!!
You need to login to post a reply.