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

2023/3/26

how to reload

envxity envxity

2023/3/26

#
i had the reload working but then i added a ammo limit and now it wont work here is the code for the class that the reload is in:
import greenfoot.*;
import greenfoot.MouseInfo;

public class Player extends Actor
{
    // all the variables for the players stats
    public final int speed = 3;
    public int firerate = 20;
    int health = 100;
    int stamina = 10;
    int cash = 0;
    int wave = 1;
    int ammo = 20;
    public int pause = 100;
    int kills = 0;
    int survivedTime = 0;
    // creates the player character
    public Player(){
        setImage(new GreenfootImage(70, 50));
        getImage().setColor(Color.YELLOW);
        getImage().fillOval(0, 0, 50, 50);
        getImage().setColor(Color.BLACK);
        getImage().fillRect(50, 20, 70, 10);
    }
    // tells the player character to use all the methods that have been created as long as the conditions are right for the given method
    public void act()
    {
        move();
        turnAround();
        reload();
        shoot();
        
    }
    // tells the actor to turn to face the mouse
    public void turnAround(){
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            if(Greenfoot.getMouseInfo() != null){
                turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
            }
        }
    }
    // tells the actor to do if the button is black and if it isnt black
     public void shoot() {
        button button = new button();
        if (Color.BLACK.equals(button.getCurrentColor())){
            manualShoot();            
        } 
        else {
            autoShoot();
        }
    }
    // returns the buttons color to determine what the firemode is
    public static int getColor()
    {
        return button.colorValue;
    }
    // one of the firemodes that is player skilled based 
    private void manualShoot()
    {        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(pause>0){
                pause --;
            }
        if(pause == 0){
                if(ammo>0){
                    if (!Greenfoot.mousePressed(null)) return;
                    turnTowards(mouse.getX(),mouse.getY());
                    Actor bullet = new bullet();
                    getWorld().addObject(bullet, getX(), getY());
                    bullet.turnTowards(mouse.getX(), mouse.getY());
                    GreenfootImage drawedImage = new GreenfootImage("gunshot.png");//the image that is drawed;
                    getImage().drawImage(drawedImage, 70, 50);
                    ammo--;
                    
                }
                pause = firerate;
        }                       
    }
    // the second fire mode that is more of a auto fire to the nearest enemy
    private void autoShoot()
    {        
        if(pause>0){
            pause --;
        }
        if(pause == 0){
            if(ammo>0){
                if (getWorld().getObjects(Zombie.class).isEmpty()) return;
                Actor closest = null;
                int closeness = 9999;
                for (Object obj : getWorld().getObjects(Zombie.class)) {
                    Actor enemy = (Actor)obj;
                    int dist = (int)Math.hypot(getX()-enemy.getX(), getY()-enemy.getY());
                    if (dist < closeness) {
                        closeness = dist;
                        closest = enemy;
                    }
                    Actor bullet = new bullet();
                    getWorld().addObject(bullet, getX(), getY());
                    bullet.turnTowards(closest.getX(), closest.getY());
                }
                GreenfootImage drawedImage = new GreenfootImage("gunshot.png");//the image that is drawed;
                getImage().drawImage(drawedImage, 70, 50);
                ammo--;
                pause = firerate;
            }
        }        
    }
    // gets the users inputs and moves the player accordingly
    public void move(){
        if(Greenfoot.isKeyDown("W")||Greenfoot.isKeyDown("UP")){
            setLocation( getX(), getY()-speed);
        }
        if(Greenfoot.isKeyDown("A")||Greenfoot.isKeyDown("LEFT")){
            setLocation( getX()-speed, getY());
        }
        if(Greenfoot.isKeyDown("S")||Greenfoot.isKeyDown("DOWN")){
            setLocation( getX(), getY()+speed);
        }
        if(Greenfoot.isKeyDown("D")||Greenfoot.isKeyDown("RIGHT")){
            setLocation( getX()+speed, getY());
        }
    }
    // tells the character to reload his gun when the user hits r
    private void reload(){
        if((Greenfoot.isKeyDown("R"))){
            pause = firerate;
            if(pause>0){
                pause --;
            }
            if(pause == 0){
                ammo = 20;
            }
        }                
    }
}
danpost danpost

2023/3/26

#
envxity wrote...
i had the reload working but then i added a ammo limit and now it wont work << Code Omitted >>
What is the variable pause being used for? It looks like it has two totally unrelated purposes -- (i) to space out one shot from another; and (ii) to delay the reloading of ammo. It cannot be used for both at the same time. Also, it looks here like the reload method has issues -- (i) assigning the value of firerate to pause on line 127 means the value of pause will never be less than 19 by line 131 (certainly no where near zero); and (ii) using isKeyDown alone means the user can hold the key down and continuously initiate reloading. Add a boolean to track the state of the key:
private boolean reloadKeyDown = false;
Then, to run tracking of the state of the key:
if (Greenfoot.isKeyDown("r") != reloadKeyDown) { // change in state of key (up or down)
    reloadKeyDown = ! reloadKeyDown; // retain new state
    if (reloadKeyDown && ammo == 0) ammo = 20; // reload when ammo out and key just pressed
}
This should reload when the "r" is pressed and will only work when the ammo is totally depleted (avoiding multiple consecutive initiations of reloading).
envxity envxity

2023/3/26

#
dan u the greenfoot goat ngl we all agree on this so on behalf of everyone that you have helped thank you
envxity envxity

2023/3/26

#
and i have another question how would i have a display that would show how much ammo you have left out of the starting amount of ammo
danpost danpost

2023/3/26

#
envxity wrote...
and i have another question how would i have a display that would show how much ammo you have left out of the starting amount of ammo
Please refer to my Value Display Tutorial scenario.
You need to login to post a reply.