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

2020/2/7

Mülltrennung/Wasteseparation

Cool Cool

2020/2/7

#
Hey can Sombody help me with finishing my Project i dont get why it oisnt working and how i can get it to work… i want to make a program taht sorts garbage and if you sort it right you get points if not you get minus points and it should get faster every 30 seconds but thats not the Problem right now… so if somebody could just help me with the timer it would be a start
//World class
import greenfoot.*;  
import java.util.List;
import javax.swing.JOptionPane;

import java.awt.Color;


public class Mülltrennung extends greenfoot.World
{    
    private int cycleCount = 0;
    private static Counter counter = new Counter("Time");
    private static boolean timerStopped = false;
    public Mülltrennung()
    {    
        super(64, 57, 10);
        setBackground("images/Background.png");
        prepare();
    }
//i tried to get a timer counting in seconds this is one solution i got:
    public void act()
    {
        if (!timerStopped)
        {
            cycleCount++;
            if (cycleCount % 60 == 0)
            {
                counter.setValue(cycleCount/60);
            }
        }
        move();
    }

    public static void stopTimer()
    {
        timerStopped = true;
    }

    public static int getTime()
    {
        return counter.getValue();
    }

    public void move()
    {
//here should be the movement of the the garbage that should be sorted the right way
    }

    public void prepare()
    {
        int random = Greenfoot.getRandomNumber(8);
        if(random == 0)
        {
            biomüll1(4,28);
        }
        else if(random == 1)
        {
            biomüll2(4,28);
        }
        else if(random == 2)
        {
            glasmüll1(4,28);
        }
        else if(random == 3)
        {
            glasmüll2(4,28);
        }
        else if(random == 4)
        {
            glasmüll3(4,28);
        }
        else if(random == 5)
        {
            kunststoff1(4,28);
        }
        else if(random == 6)
        {
            kunststoff2(4,28);
        }
        else if(random == 7)
        {
            papiermüll(4,28);
        }
//the following is part of another timer that worked but didnt count in seconds (ist copied from @danpost):
        /*Counter timeDisplay = new Counter();
        timeDisplay.setPrefix("Time: ");
        timeDisplay.adjustValue(1);
        addObject(timeDisplay, 5, 2);*/
    }

    public void biomüll1(int x, int y)
    {
        addObject(new Biomüll1(),x,y);
    }

    public void biomüll2(int x, int y)
    {
        addObject(new Biomüll2(),x,y);
    }

    public void glasmüll1(int x, int y)
    {
        addObject(new Glasmüll1(),x,y);
    }

    public void glasmüll2(int x, int y)
    {
        addObject(new Glasmüll2(),x,y);
    }

    public void glasmüll3(int x, int y)
    {
        addObject(new Glasmüll3(),x,y);
    }

    public void kunststoff1(int x, int y)
    {
        addObject(new Kunststoff1(),x,y);
    }

    public void kunststoff2(int x, int y)
    {
        addObject(new Kunststoff2(),x,y);
    }

    public void papiermüll(int x, int y)
    {
        addObject(new Papiermüll(),x,y);
    }
}
Super_Hippo Super_Hippo

2020/2/7

#
The code for adding 1 about every second seems alright. There are close to 60 act cycles a second when the speed is set to 50 = the middle (and there is no lag of course). Depending on what is happening instead of what it should, it might be clever to not have the counter and the timerStopped variable static or initialize them when creating a new Mülltrennung world.
You need to login to post a reply.