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

2015/10/20

Ammo, Stop shooting after 15 shots until ammoPickup

BroTolo BroTolo

2015/10/20

#
I'd like to have the Player shoot 15 shots, then not be able to until they collect a ammoBox which then adds 15 more shots. Please help, this has been frustrating me.
 public void Shooting()
    {
        if(Greenfoot.isKeyDown("space") && readEnter)
         {
              if(Ammo>0)
              {
               getWorld().addObject(new Bullet(getRotation()), getX() , getY());
               readEnter = false;
               Ammo--;
              }
              else
              {
                 
              } 
    }
    public void Collecting()
    {
        if(canSee(healthBox.class))
        {
            eat(healthBox.class);
            Health = Health + 2;
            Greenfoot.playSound("healthPickup.mp3");
        }
        if(canSee(ammoBox.class))
        {
            eat(ammoBox.class);
            Ammo = Ammo + 15;
            Greenfoot.playSound("ammoPickup.mp3");
        }
    }
 public Player()
    {
        image1 = new GreenfootImage("stillRight.png");
        image2 = new GreenfootImage("stillLeft.png");
        image3 = new GreenfootImage("shootRight.png");
        image4 = new GreenfootImage("shootLeft.png");
        image5 = new GreenfootImage("moveRight.png");
        image6 = new GreenfootImage("moveLeft.png");
        image7 = new GreenfootImage("bulletRight.png");
        image8 = new GreenfootImage("bulletLeft.png");
        Ammo = 15;
        Health = 2;
    }
danpost danpost

2015/10/20

#
It all looks good, except for the 'readEnter' field. There is not enough code to determine whether is value is reset back to true at any time (or to know what it is used for).
BroTolo BroTolo

2015/10/22

#
I forgot to add the part with the readEnter, but it's below. You are the reply king here! Thanks for the help, hope to see some great content from you in the future
private boolean readEnter = true;
if(!readEnter && !Greenfoot.isKeyDown("space"))
       {
        readEnter = true;
       }    
BroTolo BroTolo

2015/10/22

#
Also, I forgot to mention, how can I spawn a ammoBox once "Ammo" reaches 0? The code below just spawns ammoBoxes like crazy, I'd just like one spawned once ammo reaches 0. I'd like to add a ammoCounter in the lower right corner of the screen aswell.
 if (Ammo == 0)
        {
            getWorld().addObject(new ammoBox(), (Greenfoot.getRandomNumber(750)), 305);
        }
Super_Hippo Super_Hippo

2015/10/22

#
Shouldn't it be without the second '!'? So like this:
if(!readEnter && Greenfoot.isKeyDown("space"))
For the last problem, you could do one of the following:
private boolean ammoBoxSpawned;

if (!ammoBoxSpawned && Ammo==0)
{
    getWorld().addObject(new ammoBox(), Greenfoot.getRandomNumber(750), 305);
}
or
if (Ammo==0 && getWorld().getObjects(ammoBox.class).isEmpty())
{
    getWorld().addObject(new ammoBox(), Greenfoot.getRandomNumber(750), 305);
}
BroTolo BroTolo

2015/10/23

#
Yeah, I did something like that, thank you, I just need to make the bullet not error the program out now... Once the bullet kills the enemy is errors. But i also tried to make health on my zombie so it wouldn't just one hit him. So i tried this, and nothing.. so now i just have the bullet's code but now it errors java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:681) at greenfoot.Actor.getOneIntersectingObject(Actor.java:930) at Bullet.Damage(Bullet.java:52) at Bullet.act(Bullet.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
public Zombie1()
    {
        health = 10;
    }
    public void act() 
    {
        Life();
    }   
    public void Life()
    {
        if(canSee(Bullet.class))
        {
            health = health - 5;
            if(health == 0)
            {
                eat(Zombie1.class);
            }
        }
    }
public class Bullet extends Player
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int direction, speed;
     
    public Bullet(int dir)
    {
        direction = 0;
        speed = 3;
    }
    public void act() 
    {
     setLocation(getX() + speed, getY());
     setRotation(direction);
     move(speed);
     Remove();
     Damage();
    }
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
         return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
         return true;
        else
         return false;
    } 
    public void Remove()
    {
        if(getX() >= getWorld().getWidth() - 1)
        getWorld().removeObject(this);
        else if(getX() <= 1)
        getWorld().removeObject(this);
        else if (getY() >=getWorld().getHeight() -1)
        getWorld().removeObject(this);
        else if(getY() <= 1)
        getWorld().removeObject(this);
    }
    public void Damage()
    {
       Actor enemy = getOneIntersectingObject(Zombie1.class);
       if(enemy != null) 
       {
            getWorld().removeObject(enemy);
            if (getWorld().getObjects(Zombie1.class).size() != 0)
            {
                getWorld().addObject(new Zombie1(), 780, 327);
            }
       }
       
    }
}
danpost danpost

2015/10/23

#
The first line in the Damage method uses 'getOneIntersectingObject', which requires that the actor (the bullet executing the code) be in a world. The act method calls the Damage method immediately after calling the Remove method, which may remove the actor from the world. You could call Damage before calling Remove in the act method so nothing (or nothing requiring the bullet be in the world) is executed after removing the bullet from the world.
BroTolo BroTolo

2015/10/25

#
You are brilliant! Thank you, it now works and doesn't error!
BroTolo BroTolo

2015/10/26

#
Another thing, How can i get the bullet to be removed once it hits the enemy? It seems to error. It works as the code is, but when i got to add getWorld().removeObject(this); Right under the removing of the enemy, it then errors. I do NOT want the bullet able to keep on flying through the rest of the zombies
private int direction, speed;
     
    public Bullet(int dir)
    {
        direction = 0;
        speed = 3;
    }
    public void act() 
    {
     setLocation(getX() + speed, getY());
     setRotation(direction);
     move(speed);
     Damage();
     Remove();
    }
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
         return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
         return true;
        else
         return false;
    } 
    public void Remove()
    {
        if(getX() >= getWorld().getWidth() - 1)
        getWorld().removeObject(this);
        else if(getX() <= 1)
        getWorld().removeObject(this);
        else if (getY() >=getWorld().getHeight() -1)
        getWorld().removeObject(this);
        else if(getY() <= 1)
        getWorld().removeObject(this);
    }
    public void Damage()
    {
       Actor enemy = getOneIntersectingObject(Zombie1.class);
       if(enemy != null) 
       {
            getWorld().removeObject(enemy);
            if (getWorld().getObjects(Zombie1.class).size() != 0)
            {
                getWorld().addObject(new Zombie1(), 780, 327);
            }
       }
    }
danpost danpost

2015/10/26

#
BroTolo wrote...
Another thing, How can i get the bullet to be removed once it hits the enemy? It seems to error. It works as the code is, but when i got to add getWorld().removeObject(this); Right under the removing of the enemy, it then errors. I do NOT want the bullet able to keep on flying through the rest of the zombies < Code Omitted >
If you add that line, you will then be calling two methods ("Damage' and 'Remove' ) back to back that both can remove the bullet from the world. If the first one called removes the bullet, the second one cannot be allowed to execute as both require the bullet be in the world to execute properly. You can either put a condition on calling the second one:
if (getWorld() != null) Remove();
OR, you can (since calling 'Remove' is the last thing left in the method) add the following line between the calling of the methods:
if (getWorld() == null) return;
You need to login to post a reply.