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

2018/5/30

Need help with delay

Kachima Kachima

2018/5/30

#
Hey, I'm currently coding a little game where I want random actors to spawn with a delay that gets smaller with time, and I cant get it to work
import greenfoot.*;
public class GameBackground extends World
{
    private int score;
    private int starttime;
    private boolean x;
    public void act()
    {
        starttimer();
        lessDelay();
        checkForSpawning();
        //TestOhneDelay(); //später durch starttimer wieder ersetzen
        addScore();
        GameOver();
    }

    public GameBackground()
    {
        super(1500, 800, 1); 
        setBackground("dark-wooden-background-high-resolution-11231.jpg");
        prepare();
    }

    public void starttimer() // 5 Sekunden nach geklicktem Startbutton starten
    {
        starttime++;
        int SpawnDelay = starttime;
        if(starttime == 70 && starttime < 140)
        {
            showText("3", 800, 480);
        }
        if(starttime == 140 && starttime < 210)
        {
            showText("2", 800, 480);
        }
        if(starttime == 210 && starttime < 280)
        {
            showText("1", 800, 480);
        }
        if(starttime == 280 && starttime < 350)
        {
            showText("GO!!!", 800, 480);
        }
        if(starttime == 350)
        {
            showText("", 800, 480);
        }
        if(starttime == 370)
        {}
    }

    public void randomSpawner() // alle ca. 70 ticks/acts soll random ein Käfer iwo am Rand (oben/unten) gespawnt werden
    {

    }

    public void addScore()
    {
        if(Menü.leben > 0 && starttime >= 350)
        {
            score++;
        }
        showText("Score: " + score, 65, 10);
    }

    public void TestOhneDelay()
    {
        if(x == false)
        {
            addObject(new act2(),Greenfoot.getRandomNumber(1500)+ 1, 899); //Nur zum Test ohne verzögerten Start
            x = true;
        }
    }

    public void GameOver()
    {
        if(Menü.leben == 0)
        {
            Greenfoot.setWorld(new GameOver());
        }
    }
    private int delayDelay = 180;
    private int delay = 180;
    private int spawnTimer;
    private void checkForSpawning() // call from act method
    {
        if(starttime >= 370)
        {
            spawnTimer = (spawnTimer+1)%delay; // repeat every 3 seconds (about)
            if (spawnTimer == 0) // at each timer reset
            {
                addObject(new act2(),Greenfoot.getRandomNumber(1500)+ 1, 899);
            }
        }   
    }

    private void lessDelay() // call from act method
    {
        if(starttime >= 500)
        {
            spawnTimer = (spawnTimer+1)%delayDelay; // repeat every 3 seconds (about)
            if (spawnTimer == 0) // at each timer reset
            {
                delay = delay/900;
            }
        }   
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        heart heart = new heart();
        addObject(heart,1351,30);
        heart2 heart2 = new heart2();
        addObject(heart2,1409,37);
        heart2.setLocation(1404,29);
        heart3 heart3 = new heart3();
        addObject(heart3,1465,35);
        heart3.setLocation(1458,29);
    }
}

danpost danpost

2018/5/30

#
Kachima wrote...
Hey, I'm currently coding a little game where I want random actors to spawn with a delay that gets smaller with time, and I cant get it to work << Code Omitted >>
Line 104 looks quite suspect. If the original value is 180 and you divide that by 900, then the second value it will take on is 0 -- definitely not what you want.
Kachima Kachima

2018/5/30

#
Yes I dont really understand how the delay works, I just wanted that after the 900 value at Line 104 the spawning delay of the actors gets faster
danpost danpost

2018/5/30

#
Kachima wrote...
Yes I dont really understand how the delay works, I just wanted that after the 900 value at Line 104 the spawning delay of the actors gets faster
The following would be for a delay that gets shorter and shorter. First, you would need two fields:
private int timer; // that which counts the time of the delay
private int delayTime; // that which determine the length of the delay
Then, in your action code:
timer = (1+timer)%delayTime; // run timer
if (timer == 0) // is delay completed
{
    // do whatever timer is used for
    adjustDelay();
}
Ways to adjust the delay are given below. Something similar to one of the lines below can be placed in a new adjustDelay method or you can simply replace line 5 with a line below:
delayTime--; // simple decrementing
//
delayTime -= 2; // decreasing by any small constant
//
delayTime = delayTime*19/20; // removing a small fractional part (1/20)
Kachima Kachima

2018/5/31

#
I tried it with delayTime = delayTime*19/20; but now it says "java.lang.ArithmeticException: / by zero" after some time
danpost danpost

2018/5/31

#
Kachima wrote...
I tried it with delayTime = delayTime*19/20; but now it says "java.lang.ArithmeticException: / by zero" after some time
You have essentially run it to no delay time. If the game is not over by this point, you can stop the shortening of the delay with this (instead):
if (delayTime > 1) delayTime = delayTime*19/20;
Kachima Kachima

2018/6/2

#
and can i run it infinitely with this method? because thats the point of the game, the longer you survive the more points you get
danpost danpost

2018/6/2

#
Kachima wrote...
and can i run it infinitely with this method? because thats the point of the game, the longer you survive the more points you get
Correct.
You need to login to post a reply.