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

2017/4/1

Creating Reload time

AAA355 AAA355

2017/4/1

#
I have been trying to get my Player to shoot at intervals but I am not sure how i would ideally like it if I could shoot multiple bullets then have a reload time, currently, I can only have 1 bullet on the screen at once and I am not sure why. Player Code:
    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        fireOnCommand();
        gunReloadTime = 20;
        
       
        
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
       
        
        }
        public void fireOnCommand()
        {
            if(Greenfoot.isKeyDown("space"))
            {
                World myWorld = getWorld();
                 myWorld.addObject(bullet, 0, 0);
                 bullet.setLocation(getX(), getY());
                 bullet.setRotation(getRotation());
            }
            
        }
        /**
     * Set the time needed for re-loading the rocket's gun. The shorter this time is,
     * the faster the rocket can fire. The (initial) standard time is 20.
     */
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
Bullet Code:
/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends bullets
{
     public int speed = 10;
    
    
    /**
     * Act - do whatever the missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(15);
        Actor Robber = getOneIntersectingObject(Robber.class);
        
        if(isTouching(Robber.class))
        {
            getWorld().removeObject(Robber);
            getWorld().addObject(new Robber(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            
            getWorld().removeObject(this);

        }
        }
}
Super_Hippo Super_Hippo

2017/4/1

#
You didn't copy it, but you probably have this line at the beginning of the Player class:
Bullet bullet = new Bullet();
In line 36, you add this bullet to the world. When it is already in the world, it will change its location. (The same bullet, not a new one.) The player doesn't need to keep track of its bullets, so you should remove this field. When adding it, you need to create a new bullet.
Actor bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(getRotation());
When you want to have multiple shots before reloading, you need a field to hold how many shots are left, optional how many are maximum, how much time is left until reloading has finished and optional how long reloading should take.
private final int reloadTime = 50,
                  maxShots = 6;

private int reloadTimer = 0,
            shotsLeft = maxShots;

private boolean shooting = false;


//in act
if (reloadTimer > 0)
{
    if (--reloadTimer == 0) shotsLeft = maxShots;
    else return;
}

if (!shooting && Greenfoot.isKeyDown("space"))
{
    Actor bullet = new Bullet();
    getWorld().addObject(bullet, getX(), getY());
    bullet.setRotation(getRotation());
    shooting = true;
    if (--shotsLeft == 0) reloadTimer = reloadTime;
}
else if (!Greenfoot.isKeyDown("space")) shooting = false;
AAA355 AAA355

2017/4/2

#
thank you so much for replying, I can now have multiple bullets on screen except there is still no reload time. not sure why.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends people
{
    
    
    Actor bullet = new Bullet();
    private int gunReloadTime;
    private int reloadDelayCount;
    private final int reloadTime = 10000,
                  maxShots = 6;
    private int reloadTimer = 10000,
            shotsLeft = maxShots;
    private boolean shooting = false;
    
    /**
     * Act - do whatever the robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(-5);
        }
        
        if (reloadTimer > 5000)
        {
            if (--reloadTimer == 5000) shotsLeft = maxShots;
            else return;
        }
        
        if (!shooting && Greenfoot.isKeyDown("space"))
        {
            Actor Bullet = new Bullet();
            getWorld().addObject(Bullet, getX(), getY());
            Bullet.setRotation(getRotation());
            shooting = true;
            if (--shotsLeft == 0 ) reloadTimer = reloadTime;
        }
        else if (!Greenfoot.isKeyDown("space")) shooting = false;

        }
    }
i messed around with the numbers and hwen i had them high enough there was a reload time of like 60 seconds but i as unable to move my player during this reload time.
AAA355 AAA355

2017/4/2

#
nevermind, i was able to fix the movement issues and the reload time is perfect, thank you so much for the help. :)
You need to login to post a reply.