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

2020/8/18

I Require Assitance

FrostyBoi FrostyBoi

2020/8/18

#
Hello I am new to Greenfoot and am currently working on a TD project the problem is that i want a tower to shoot for a certain amount of time then pause then start again Plz Anyone
danpost danpost

2020/8/18

#
FrostyBoi wrote...
Hello I am new to Greenfoot and am currently working on a TD project the problem is that i want a tower to shoot for a certain amount of time then pause then start again Plz Anyone
Add an int field as a timer. Increment to a number equal to acts required for one cycle of shooting and not shooting, then reset to zero. With lower values for shooting, if in that range and divisible by some number, say 60 for shooting each second, fire. So, let us say you want to shoot 4 shots in 4 seconds (one per second), then not shot for 6 seconds, repeating. Assuming normal running speed for the scenario (speed slider in middle), then there are about 60 acts executed per second. Therefore:
/** instance field */
private int shotTimer;

/** in act */
shooting();

/** shooting method */
private void shooting()
{
    shotTimer = (shotTimer+1)%600; // 600 is 60 (acts per second) times the sum of 4 plus 6 (seconds per cycle)
    if (shotTimer < 240 && shotTimer%60 == 0) // timer in shooting range and of whole second value
    {
        System.out.println("Shot fired"); // replace to create and fire a shot somewhere
    }
}
You need to login to post a reply.