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

2017/5/2

How to make ammo?

wowi132 wowi132

2017/5/2

#
Hi I have the following code, which allows my main character to fire bullets. I tried to make an integer, called ammo, and giving it the value 15. I then tried to incorporate into my code, that ammo integer -1 whenever i fire a bullet. The shooting-code works fine, but for some reason i have unlimited ammo - in other words, my ammo integer isn't connected to the shooting code. Can anyone help me? it would be much appreciated
int ammo;
    public void Ditzel()
    {
        ammo=5;
    }
    private boolean spaceDown;
    public void shoot()
    {
        if(!spaceDown && Greenfoot.isKeyDown("space"))
        {
            if(Munition()==true)
            {
                spaceDown=true;
                Bullet bullet = new Bullet();
                bullet.setRotation(getRotation());
                getWorld().addObject(bullet, getX(), getY());
                ammo--;
            }
        }
        if(spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown=false;
        }
    }
    public boolean Munition()
    {
        if(ammo>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
danpost danpost

2017/5/2

#
You need to show the act method, also.
wowi132 wowi132

2017/5/2

#
Thank you so much here is the entire code for my actor
ublic void act() 
    {
        moveAndTurn();
        shoot();
        Ditzel();
    } 
    
    public void moveAndTurn()
    {
       if (Greenfoot.isKeyDown("left"))
       {
           setRotation(180);
           if(getOneObjectInFront(Wall.class)==null)
           move(3);
        }
       if (Greenfoot.isKeyDown("right"))
       {
           setRotation(0);
           if(getOneObjectInFront(Wall.class)==null)
           move(3);
        }
       if (Greenfoot.isKeyDown("up"))
       {
           setRotation(270);
           if(getOneObjectInFront(Wall.class)==null)
           move(3);
        }
        if (Greenfoot.isKeyDown("down"))
       {
           setRotation(90);
           if(getOneObjectInFront(Wall.class)==null)
           move(3);
        }
    }
    int ammo;
    public void Ditzel()
    {
        ammo=5;
    }
    private boolean spaceDown;
    public void shoot()
    {
        if(!spaceDown && Greenfoot.isKeyDown("space"))
        {
            if(Munition()==true)
            {
                spaceDown=true;
                Bullet bullet = new Bullet();
                bullet.setRotation(getRotation());
                getWorld().addObject(bullet, getX(), getY());
                ammo--;
            }
        }
        if(spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown=false;
        }
    }
    public boolean Munition()
    {
        if(ammo>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    

private Actor getOneObjectInFront(Class c)
{
    GreenfootImage myImage = getImage();
    int distanceToFront = myImage.getWidth();
    int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
    int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
    return (getOneObjectAtOffset(xOffset, yOffset, c));
}
}
danpost danpost

2017/5/2

#
Every act cycle, the last thing done is executing the 'Ditzel' method. This method sets the value of 'ammo' to 5. No wonder it never gets down to zero.
wowi132 wowi132

2017/5/2

#
I am not very clever, i am sorry. Where do i put it then?
wowi132 wowi132

2017/5/2

#
I mean where do i put the value of the ammo, if i wish to start out with 5 ammo, but not making it cycle
danpost danpost

2017/5/2

#
wowi132 wrote...
where do i put the value of the ammo, if i wish to start out with 5 ammo, but not making it cycle
Change line 35 to:
int ammo = 5;
and remove lines 5 and lines 36 through 39.
wowi132 wowi132

2017/5/2

#
Thank you very very much for your help Its for a school project, and you pretty much saved my entire project :)
You need to login to post a reply.