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

2018/11/23

Do not spawn bullet

greenfootUser343 greenfootUser343

2018/11/23

#
Hello, I would like to have my game not to spawn bullets if the ammoCount variable = 0; This is what I have in my Car Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mclaren here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mclaren  extends Characters
{
    /**
     * Act - do whatever the Car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    // instance Variables
    private int damage;
    private int gunDelay;
    private int minGunDelay;
    private int ammoCount = 5; 
    
    public Mclaren()  // sets damage of the car to 0 also sets gunDelay and minGunDelay. Prob should have made minGunDelay Static in future.
      {
         damage = 0;
         gunDelay = 0;
         minGunDelay = 20;
         
      }
    
    public void act() // main part of the car that keeps running.
    {
        collectAmmo();
        checkKeyPress();
        checkCollision();
    }  
   
    public void checkCollision() // increases damage of car when it hits a Vehicle.
    {
        Actor actor = getOneIntersectingObject(Mclaren.class);
        if (actor != null)
        {
            damage++;
            Greenfoot.playSound("crash.wav");
        }
        if (damage >= 100)
        {
            Greenfoot.stop();
        }
               
    }
    
    public void moveLeft() // moves car left but stops it before it runs off road.
    {
        if(getX() <= 410)
        {
        setLocation(getX() - 2, getY());
        }
    }
    
    public void moveRight()  // moves the car right but stops it before it runs off road.
    {
        if(getX() < 400)
        {
        setLocation(getX() + 2, getY());
        }
    }
    public void moveUp()  // moves car up the screen
    {
        setLocation(getX(), getY() - 2);
    }
    public void moveDown() // moves car down the screen.
    {
        setLocation(getX(), getY() + 2);
    }
    public void collectAmmo(){
        if(canSee(Ammo.class)){
            collectItem(Ammo.class);
            ammoCount ++;
        }
    }
    
    public void fire()
    {
        if(gunDelay >= minGunDelay)   // this lets u fire the gun and sets gun dealy to 0 so it has some interuption.
        {
            Rocket b = new Rocket();
            getWorld().addObject(b, getX(), getY());
            b.move(0);
            gunDelay = -50;
            ammoCount --;
        }
        else if (ammoCount == 0) {
            
            
            
            
        }
    }
    
          
    
    public void checkKeyPress()
    {
        if(Greenfoot.isKeyDown("left")) // move car left
        {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("right")) // move car right
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("up")) // Move car up
        {
           
            moveUp();
        }
        if(Greenfoot.isKeyDown("down")) // Move car down
        {     
            moveDown();
        }
         if(Greenfoot.isKeyDown("space")) //this is my shoot key
         {
            fire();
         }        
        // work in progress i want the car to change and start playing music. it has a problem where it will keep playing the music need some sort of delay.
        String key = Greenfoot.getKey(); 
        if(key != null)
        {
            if(key.equalsIgnoreCase("e"))
            {
                Greenfoot.playSound("spyhunter.wav"); 
            }
        }
        gunDelay++;  //added this for shooting 
    }   
    
}
I have thought to use an else if in the fire method, but unsure how to structure it. Thanks in advance.
danpost danpost

2018/11/23

#
greenfootUser343 wrote...
I would like to have my game not to spawn bullets if the ammoCount variable = 0; << Code Omitted >> I have thought to use an else if in the fire method, but unsure how to structure it.
The else in line 92 is basically saying "If you did not fire a bullet and no bullets are available, then ...". The firing of a bullet was already done without regard to ammo count; therefore an else is not what you want. You just want another restriction placed on firing a bullet. Change line 84 to:
if (ammoCount > 0 && gunDelay >= minGunDelay)
greenfootUser343 greenfootUser343

2018/11/23

#
Thank you Dan, that worked perfectly.
You need to login to post a reply.