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

2019/3/1

Back and forth loop for countdowns

Notted Notted

2019/3/1

#
What I want to do is create some sort of a loop that goes back and forth between to states. One will countdown, the other count up. My code as of now:
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
private int playersSpeed = 3;
    private int shotCountDownPlayer = 60;
    private int playerRateOfFire = 2;
    private boolean canShoot;
    public void act()
    {
        playerControlsMovement();
        playerControlsFiring();
        playersShooting();
    }   
    private void playerControlsMovement()
    {        
       if (this != null && Greenfoot.isKeyDown("a"))
       {
           move(-playersSpeed);
       }
       if (this != null && Greenfoot.isKeyDown("d"))
       {
           move(playersSpeed); 
       }
    }
    private void playerControlsFiring()
    
       MyWorld spaceInvWorld = (MyWorld) getWorld();
       worldBullet playersBullet = new worldBullet();
       int turretY = getY()-getImage().getHeight()/2;
       if (this != null && Greenfoot.isKeyDown("space") && canShoot == true)
       {
           spaceInvWorld.addObject(playersBullet, getX(), turretY);
       }
    }
    private void playersShooting()
    {     
        while (shotCountDownPlayer >= 60)
        {
            shotCountDownPlayer = shotCountDownPlayer - playerRateOfFire;
            canShoot = true;
        }
        if (shotCountDownPlayer <= 0)
        {
            shotCountDownPlayer = shotCountDownPlayer + playerRateOfFire;
            canShoot = false
        }
    }
}
This does not work. shotCountDownPlayer ticks down by playerRateOfFire, but stops after one decerment.
danpost danpost

2019/3/1

#
In lines 13, 17 and 27, remove 'this != null &&'. this is never null. I do not see how shotCountDownPlayer will ever be anything other than initial value of 60 or its only adjusted, and permanently stuck at, value of 58. One iteration of the while loop will be enough to make the condition to continue the loop false and nothing is done to the field once its value is between 1 and 59. Try separating the value changing from the bounds checks and just negate the playerRateOfFire field to reverse the direction of change in the value of the field.
Notted Notted

2019/3/1

#
I'm not sure how to do that.
danpost danpost

2019/3/1

#
Notted wrote...
I'm not sure how to do that.
1
2
3
4
5
6
shotCountDownPlayer -= playerRateOfFire;
if (shotCountDownPlayer%60 == 0)
{
    playerRateOfFire *= -1;
    canShoot = !canShoot;
}
Notted Notted

2019/3/5

#
Thanks, but this does not work. playerTurret simply does not fire at all.
danpost danpost

2019/3/5

#
Notted wrote...
Thanks, but this does not work. playerTurret simply does not fire at all.
Try this:
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
private int playersSpeed = 3;
private int shotCountDownPlayer = 60;
private int playerRateOfFire = 2;
 
public void act()
{
    playerControlsMovement();
    playerControlsFiring();
    playersShooting();
}   
 
private void playerControlsMovement()
{        
    if (Greenfoot.isKeyDown("a")) move(-playersSpeed);
    if (Greenfoot.isKeyDown("d")) move(playersSpeed); 
}
 
private void playerControlsFiring()
{
    if (Greenfoot.isKeyDown("space") && shotCountDownPlayer == 0)
    {
        worldBullet playersBullet = new worldBullet();
        int turretY = getY()-getImage().getHeight()/2;
        getWorld().addObject(playersBullet, getX(), turretY);
        shotCountDownPlayer = 60;
    }
}
 
private void playersShooting()
{
    if (shotCountDownPlayer > 0) shotCountDownPlayer -= playerRateOfFire;
}
Notted Notted

2019/3/5

#
I don't want a line/stream of bullets. Basically what I want is time in between the shots. That's why canShoot is there.
danpost danpost

2019/3/5

#
Notted wrote...
I don't want a line/stream of bullets. Basically what I want is time in between the shots. That's why canShoot is there.
There will be a delay between shots. I only got rid of canShoot because the counter value gives the same information.
Notted Notted

2019/3/6

#
Nevermind. This seems to work.
1
2
3
4
5
6
7
8
9
10
11
private void playersShooting()
    {     
        if (shotCountDownPlayer > 0)
        {
            shotCountDownPlayer -= playerRateOfFire;
        }
        else if (shotCountDownPlayer == 0)
        {
            shotCountDownPlayer = 60;
        }
    }
Notted Notted

2019/3/7

#
Ok. Now we need to do this with the aliens. The same principle applies here. Aliens do not have controls, but I believe the same code used for shooting can be applied for the aliens.
danpost danpost

2019/3/7

#
Notted wrote...
Ok. Now we need to do this with the aliens. The same principle applies here. Aliens do not have controls, but I believe the same code used for shooting can be applied for the aliens.
It should work.
Notted Notted

2019/3/7

#
danpost wrote...
Notted wrote...
Ok. Now we need to do this with the aliens. The same principle applies here. Aliens do not have controls, but I believe the same code used for shooting can be applied for the aliens.
It should work.
I'm not sure if it does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void enemeyShoot()
    {
       xenoBullet aliensBullet = new xenoBullet();
       int alienY = getY()+getImage().getHeight()/2;
       MyWorld spaceInvWorld = (MyWorld) getWorld();
      if (shotCountDownAlien > 0)
        {
            shotCountDownAlien -= rateOfFireAlien;
            spaceInvWorld.addObject(aliensBullet, getX(), alienY);
        }
        else if (shotCountDownAlien == 0)
        {
            shotCountDownAlien = 40;
        }
       
    }
Super_Hippo Super_Hippo

2019/3/7

#
I think you should add the bullet in the elseif-block and not in the if-block. There is also no reason to have lines 3-5 executed when they are not needed.
You need to login to post a reply.