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

2018/5/16

How do I make shot delays?

Prox276 Prox276

2018/5/16

#
public void fire() 
    {
    projectile bullet = new projectile();
    getWorld().addObject(bullet, getX(), getY());
   }

if (Greenfoot.isKeyDown("space"))
    {   
     fire();
    }
danpost danpost

2018/5/16

#
For a shot delay (regular repeated firing at a regulated rate when holding key down), you would use an int field to time the gap between firings:
private int shotDelay;
At any time it has a positive value, it should be decremented:
if (shotDelay > 0) shotDelay--;
Then, a second condition can be added to your line 7 above:
if (shotDelay == 0 && Greenfoot.isKeyDown("space"))
Finally, when a shot is fired, the shot delay will need initiated:
shotDelay == 30; // adjust as needed
Those are the pieces. Hopefully, you will be able to place them properly into your code.
Prox276 Prox276

2018/5/16

#
Thank you for your help, but I don't know exactly where to put the second and forth pieces in my code.
danpost danpost

2018/5/16

#
Prox276 wrote...
Thank you for your help, but I don't know exactly where to put the second and forth pieces in my code.
The second piece can be the first line in your act method and the fourth one goes along with line 9.
Prox276 Prox276

2018/5/16

#
Thank you! Now it works.
You need to login to post a reply.