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

2018/1/24

Delays

MaxBest MaxBest

2018/1/24

#
Hello, in Greenfoot I currently have the problem of delaying things, such as a gun firing. Here is some code:
public void act() {
      Fire();
}
and,
private void Fire() {
        if (Greenfoot.isKeyDown("space")) {
               // Some stuff that isn't really relevant to fire
        }
    }
Now this does work but it is instantly firing without delay, which is a bit annoying. If possible I would quite like it to have a variable as a delay that can be changed later on in other scripts. Thank you for any help possible, Max
Super_Hippo Super_Hippo

2018/1/24

#
Create an int variable in which you save the delay. For example 2 seconds (=around 110 act cycles in normal speed). Create another int variable used as a timer. Decrease (/increase) the variable if it is above zero (/below the delay variable). If not, check if space is pressed to create the next shot and reset the timer to the delay variable (/zero)
You need to login to post a reply.