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

2022/4/14

how to make bullets stop when ammo is 0

Alex20 Alex20

2022/4/14

#
title also heres my pl2 code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class plr2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class plr2 extends Actor
{
    /**
     * Act - do whatever the plr2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int health = 50;
    boolean hitammo = false;
     private final int GRAVITY = 1;
    private final int STEP = 10;
   int vspeed = 0;
   int accel = 0;
   boolean isAlive = true;
   boolean clockstop;
   private int timeCounter;
   private int timeElapsed;
   private Actor timerDisplay;
   int count = 0;
   
    public void act() 
    {
        fall();
        checkfalling();
        if(Greenfoot.isKeyDown("up")  && isOnSolidGround()) jump();
        if(Greenfoot.isKeyDown(",")  && isOnSolidGround()) highjump();
        move();
        fireea2();
        code();
        ammocolect();
        
        
    }  
    
    public void ammocolect() {
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(isTouching(enemy.class))
         {
             
        World myWorld = getWorld();
         Platform platform = (Platform)myWorld;
         Count count = platform.getCount();
         count.addCount();
       }
     if(enemy!=null) {
         
         World myWorld = getWorld();
         Platform platform = (Platform)myWorld;
         Count count = platform.getCount();
         count.addCount();
        } 
    }
    int ea2 = 5;
    public boolean Munition()
    {
        if(ea2>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
      void code() {
    
        hitbyammo();
        if(health <= 0){isAlive = false;}
        if (isAlive == true){
          fall();
          checkfalling();
        if(Greenfoot.isKeyDown("up")  && isOnSolidGround())
        {
            jump();
        }
        move();
        }
        if(isAlive = false){
            
        }
     }
     public void fireea2() {
        if(Greenfoot.isKeyDown(".")) {
               
           ea2 projectile = new ea2();
           getWorld().addObject(projectile, getX() , getY());
           

        }
    }
     void hitbyammo() {
        Actor projectile = getOneIntersectingObject(ammo.class);
         if(projectile != null && !hitammo) {
             health --;
             hitammo = true;
             getWorld().removeObject(projectile);
            }
            else if (!isTouching(ammo.class)) {
                hitammo = false;
            }
            if(health <= 0) {
                World myWorld = getWorld();
            Gameover gameover = new Gameover();
             myWorld.addObject(gameover, myWorld.getWidth()/3, myWorld.getHeight()/2);
                getWorld().removeObject(this);
            }
      }
       public  void fall() {
        setLocation(getX(), getY() + vspeed); 
        if (isOnSolidGround()) {
           vspeed = 0;
        
              while (isOnSolidGround()) {
            setLocation(getX(), getY() - 1);
           }
           setLocation(getX(), getY() + 1);
          }
          else if(vspeed < 0 && didbumphead()) {
            vspeed = 0;
            
            while (didbumphead()) {
                setLocation(getX(), getY() + 1);
            }
        }
        else{ vspeed += GRAVITY;}
   }
   public void checkfalling() {
       if(!isTouching(ground.class)) {
           vspeed++;
        }
        else
        vspeed = 0;
       
    }
    public void jump() {
        vspeed = -30;
   }
   public void highjump() {
     vspeed = -45;  
    }
    public void move() { 
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("left") && canmoveleft())x-= STEP;
        if(Greenfoot.isKeyDown("right") && canmoveright())x+= STEP;
         setLocation(x,y); 
   }
     public boolean isOnSolidGround() {
        boolean isonground = false;
        
        if (getY() > getWorld().getHeight() - 40) isonground = true;
        
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, ground.class) != null ||
           getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, ground.class) != null)
         isonground = true;
        
        return isonground;
     }
    public boolean didbumphead() {
        boolean bumpedhead = false;
        
         int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2, imageHeight / -2, ground.class) != null ||
           getOneObjectAtOffset(imageWidth / 2, imageHeight / -2, ground.class) != null)
           bumpedhead = true;
           
        return bumpedhead;
   }
   public boolean canmoveleft() {
        boolean canmoveleft = true;
        
         int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2 - STEP, imageHeight / -2, ground.class) != null ||
           getOneObjectAtOffset(imageWidth / - 2 - STEP, imageHeight / 2 - 1, ground.class) != null)
           canmoveleft = false;
           
        return canmoveleft;
    }
        public boolean canmoveright() {
        boolean canmoveright = true;
        
         int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
         if(getOneObjectAtOffset(imageWidth / 2 + STEP, imageHeight / -2, ground.class) != null ||
           getOneObjectAtOffset(imageWidth / 2 + STEP, imageHeight / 2 - 1, ground.class) != null)
           canmoveright = false;
           
        return canmoveright;
       
    }   
}
danpost danpost

2022/4/22

#
You not only need to stop when no ammo is available, but you also must only fire when the key is initially pressed (not supposed to fire continually while the key is in the down state -- aka "pressed"). Therefore, you need to track the state of the key so that you can determine when its state actually changes. You will need a boolean field to retain the current state of the firing key:
private boolean fireKeyDown = false;

public void fireea2()
{
    if (fireKeyDown != Greenfoot.isKeyDown("."))
    {
        fireKeyDown = ! fireKeyDown;
        if (fireKeyDown && ea2 > 0)
        {
            getWorld().addObject(new ea2(), getX() , getY());
            ea2--;
        }
    }
}
You need to login to post a reply.