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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * 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:
1
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.
1
2
3
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.