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

2015/11/20

How delay a method like the proton wave on asteroids?

4SeaBass 4SeaBass

2015/11/20

#
How do I get it to not be able to do the proton wave for 30 seconds after you use it?
Royalblue64 Royalblue64

2015/11/20

#
For this, you would use a timer. By default, Greenfoot scenarios run at 50 "acts" per second, so if you wanted to create a timer for 30 seconds, it would be for 1500 acts. To do this, you could have something like the following:
private int reloadTime = 0;

public void act()
{
	//All of your other code for the player character
	
	checkInput();
}

private void checkInput()
{
	//All of your other code for controls
	
	if (Greenfoot.isKeyDown("Space") && reloadTime <= 0)
	{
		reloadTime = 1500;
		
		fireProtonWave(); //Whatever method you're using to shoot
	}
	
	reloadTime--;
}
Feel free to respond with any problems or questions :)
winnerpig winnerpig

2015/11/22

#
Royalblue64 wrote...
By default, Greenfoot scenarios run at 50 "acts" per second,
How do you know that?
danpost danpost

2015/11/22

#
winnerpig wrote...
< Quote Omitted > How do you know that?
I believe that this would depend on the speed of your system. I wrote the following world class to test my system:
import greenfoot.*;

public class TestWorld extends World
{
    long startTime;
    int acts;
    int seconds;
    
    public TestWorld()
    {
        super(1, 1, 1);
    }
    
    public void started()
    {
        startTime = System.currentTimeMillis();
    }
    
    public void act()
    {
        acts++;
        if ((System.currentTimeMillis()-startTime)/1000 > seconds)
        {
            seconds++;
            System.out.println("acts per second: "+acts);
            acts = 0;
        }
    }
}
Create a new scenario with a new subclass of world and copy/paste the code above replacing the world class code of the scenario; compile and run. My system runs it at about 61 acts cycles per second. To confirm my belief, please post your results.
You need to login to post a reply.