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

2020/2/14

Mülltrennung/Wasteseparation

Cool Cool

2020/2/14

#
Hey So I got a Problem with my code: I want to make a game where you need to sort the trash that randomly appears at the start of the track and you should decide wether it should go the upper way (press "up") or the other way (press "down"). If you decide right you get points if not you get minus points. The game should last 240s and should stop if you got 0 points. I don´t know what i did wrong and would need help!!! Thisis the Error shown in the konsole:
java.lang.OutOfMemoryError: Java heap space
	at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
	at java.awt.image.Raster.createPackedRaster(Raster.java:467)
	at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
	at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186)
	at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:186)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:130)
	at greenfoot.World.setBackground(World.java:164)
	at greenfoot.World.setBackground(World.java:204)

//--> From this line on it´s is red, before ist just GreyT

	at Muelltrennung.<init>(Muelltrennung.java:21) 
	at Glasmüll1.<init>(Glasmüll1.java:11)
	at Muelltrennung.glasmüll1(Muelltrennung.java:201)
	at Muelltrennung.prepare(Muelltrennung.java:54)
	at Muelltrennung.<init>(Muelltrennung.java:22)
	at Glasmüll3.<init>(Glasmüll3.java:11)
	at Muelltrennung.glasmüll3(Muelltrennung.java:211)
	at Muelltrennung.prepare(Muelltrennung.java:62)
	at Muelltrennung.<init>(Muelltrennung.java:22)
	at Papiermüll.<init>(Papiermüll.java:11)
	at Muelltrennung.papiermüll(Muelltrennung.java:226)
	at Muelltrennung.prepare(Muelltrennung.java:74)
	at Muelltrennung.<init>(Muelltrennung.java:22)
	at Biomüll2.<init>(Biomüll2.java:11)
	at Muelltrennung.biomüll2(Muelltrennung.java:196)
	at Muelltrennung.prepare(Muelltrennung.java:50)
	at Muelltrennung.<init>(Muelltrennung.java:22)
	at Biomüll2.<init>(Biomüll2.java:11)
	at Muelltrennung.biomüll2(Muelltrennung.java:196)
	at Muelltrennung.prepare(Muelltrennung.java:50)
	at Muelltrennung.<init>(Muelltrennung.java:22)
	at Kunststoff2.<init>(Kunststoff2.java:11)
	at Muelltrennung.kunststoff2(Muelltrennung.java:221)
	at Muelltrennung.prepare(Muelltrennung.java:70)
java.lang.OutOfMemoryError: Java heap space
The background is this image . The notes are in german but thats because i startet it all with the scenario I´m using at School (in Germany), so you shouldn´t care about that:
/*World Subclass*/

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import javax.swing.JOptionPane;

/**
 * @author Lasse Rosenbaum
 * @version 14.02.2020
 */
public class Muelltrennung extends greenfoot.World
{    
    private Timer timer = new Timer();
    private Points point = new Points();
    public static int points;
    /**
     * Präpariert die Welt mit den Maßen x=64, y=57 und der Kastengröße=10 Pixel
     */
    public Muelltrennung()
    {    
        super(64, 57, 10);
        setBackground("images/Background.png");
        prepare();
        timer.setTimer();
        point.setPoints();
        Greenfoot.setSpeed(50);
    }

    public void act()
    {
        timer.Timer();
        point.Points();
    }

    /**
     *Prepare setzt zufällig ein Müllstück an die Stelle des Starts(x=4,y=28)
     *!ACHTUNG! wenn "super" bei der Methode "Mülltrennung()" geändert wird,
     *          muss auch x und y geändert werden!
     */
    public void prepare()
    {
        int random = Greenfoot.getRandomNumber(8);
        int x = 4;
        int y = 28;
        if(random == 0)
        {
            biomüll1(x,y);
        }
        else if(random == 1)
        {
            biomüll2(x,y);
        }
        else if(random == 2)
        {
            glasmüll1(x,y);
        }
        else if(random == 3)
        {
            glasmüll2(x,y);
        }
        else if(random == 4)
        {
            glasmüll3(x,y);
        }
        else if(random == 5)
        {
            kunststoff1(x,y);
        }
        else if(random == 6)
        {
            kunststoff2(x,y);
        }
        else if(random == 7)
        {
            papiermüll(x,y);
        }
    }

    /*
     Ab hier kommt:
     -Timer
     -Punkte
     -Actor setzen
     */

    private class Timer extends Actor
    {
        private Counter counter = new Counter();
        private int actCycles;
        private int timeElapsed;
        private String prefix = "";
        private x X = new x();
        public void Timer()
        {
            actCycles = (actCycles+1)%60;
            if(actCycles == 0)
            {
                timeElapsed++;
                updateTimerDisplay();
                if((timeElapsed%30) == 0)
                {
                    X.wrong--;
                    X.right++;
                }
                if(timeElapsed == 240)
                {
                    Greenfoot.stop();
                }
            }
        }

        private void updateTimerDisplay()
        {
            String text = prefix+timeElapsed;
            counter.setImage(new GreenfootImage(text, 24, Color.BLACK, new Color(0,0,0,0)));
            repaint();
        }

        public void setTimer()
        {
            counter.setPrefix("Time: ");
            counter.adjustValue(0);
            addObject(counter, 5, 2);
            updateTimerDisplay();
            repaint();
        }
        private class Counter extends Actor
        {
            public void adjustValue(int amount)
            {
                timeElapsed += amount;
            }

            public void setPrefix(String text)
            {
                prefix = text;
                if(prefix == null)
                {
                    prefix = "";
                }
            }
        }
    }
    
    private class Points extends Actor
    {
        private Point point = new Point();
        private String prefix = "";
        public void Points()
        {
            updatePointsDisplay();
            if(points == 0)
            {
                Greenfoot.stop();
            }
        }

        private void updatePointsDisplay()
        {
            String text = prefix+points;
            point.setImage(new GreenfootImage(text, 24, Color.BLACK, new Color(0,0,0,0)));
            repaint();
        }

        public void setPoints()
        {
            point.setPrefix("Points: ");
            point.adjustValue(10);
            addObject(point, 10, 2);
            repaint();
        }
        private class Point extends Actor
        {
            public void setPrefix(String text)
            {
                prefix = text;
                if(prefix == null)
                {
                    prefix = "";
                }
            }
            
            public void adjustValue(int amount)
            {
                points = amount;
            }
        }
    }

    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);
    }
}


//Actor Subclass and superclass for other Classes from Actor

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)

public class x extends Actor
{
    public int wrong = -1;
    public int right = 2;
    public int x = 0;
    public int y = 0;
    public int points = Muelltrennung.points;
    public boolean move1 = false;
    public boolean move2 = false;
    public boolean move3 = false;
    public boolean move4 = false;
    public boolean move5 = false;
    public boolean move6 = false;
    public boolean move7 = false;

    public void moveReset()
    {
        move1 = false;
        move2 = false;
        move3 = false;
        move4 = false;
        move5 = false;
        move6 = false;
        move7 = false;
    }

    public boolean down()
    {
        boolean down = false;
        down = Greenfoot.isKeyDown("down");
        return down;
    }

    public boolean up()
    {
        boolean up = false;
        up = Greenfoot.isKeyDown("up");
        return up;
    }

    public void move1()
    {
        x= getX();
        y= getY();
        while(x != 12 && y == 28)
        {
            x=getX()+1;
            y=getY();
            setLocation(x,y);
        }
        move1 = true;
    }

    public void move2()
    {
        x= getX();
        y= getY();
        while(x != 20 && y < 29 && y > 20)
        {
            x=getX()+8;
            y=getY()-2;
            setLocation(x,y);
        }
        move2 = true;
    }

    public void move3()
    {
        x= getX();
        y= getY();
        while(x < 28 && y < 36 && y > 20)
        {
            x=getX()+8;
            y=getY()+2;
            setLocation(x,y);
        }
        move3 = true;
    }

    public void move4()
    {
        x=getX();
        y=getY();
        while(x < 32 && y < 28 && y > 7)
        {
            x=getX()+4;
            y=getY()-3;
            setLocation(x,y);
        }
        move4 = true;
    }

    public void move5()
    {
        x=getX();
        y=getY();
        while(x < 34 && y == 20)
        {
            x=getX()+1;
            y=getY();
            setLocation(x,y);
        }
        move5 = true;
    }

    public void move6()
    {
        x=getX();
        y=getY();
        while(x < 34 && y == 36)
        {
            x=getX()+1;
            y=getY();
            setLocation(x,y);
        }
        move6 = true;
    }

    public void move7()
    {
        x=getX();
        y=getY();
        while(x < 32 && y > 28 && y > 7)
        {
            x=getX()+4;
            y=getY()+3;
            setLocation(x,y);
        }
        move7 = true;
    }
}

//Subclass of "x" so subsubclass of Actor (there are 8 in total but ist all the same with different values

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import javax.swing.JOptionPane;
/**
 * Write a description of class Biomüll here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Biomüll1 extends x
{
    private Muelltrennung muelltrennung = new Muelltrennung();

    public void act() 
    {
        move1();
        if(down()&&move1)
        {
            move3();
            if(up()&&move3)
            {
                move6();
                points += right;
                moveReset();
                getWorld().removeObject(this);
            }
            else
            {
                points += wrong;
                moveReset();
                getWorld().removeObject(this);
            }
        }
        else
        {
            points += wrong;
            muelltrennung.prepare();
            moveReset();
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2020/2/14

#
Line 381 is the problem -- remove that line from all subclasses of x. Use the following for line 406 in those classes:
((Muelltrennung)getWorld()).prepare();
With 381, you had an infinite loop. World creates actor, actor creates new world; new world creates actor -- etc. Well, no really a loop, but you get the picture (I hope).
Cool Cool

2020/2/15

#
danpost wrote...
Line 381 is the problem -- remove that line from all subclasses of x. Use the following for line 406 in those classes:
((Muelltrennung)getWorld()).prepare();
With 381, you had an infinite loop. World creates actor, actor creates new world; new world creates actor -- etc. Well, no really a loop, but you get the picture (I hope).
Thank you for your help… I get what was the error and now it kind of works… But I got a new Problem: I want the progrm to stop after the object did move1() until I either press "up" or "down" and then continues. I changed the code a bit: Actor x:
public class x extends Actor
{
    private boolean down = false;
    private boolean up = false;

    public boolean down()
    {
        while(!down)
        {
            if(Greenfoot.isKeyDown("down"))
            {
                down = true;
            }
            else if(Greenfoot.isKeyDown("up"))
            {
                return false;
            }
        }
        return down;
    }

    public boolean up()
    {
        while(!up)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                up = true;
            }
            else if(Greenfoot.isKeyDown("down"))
            {
                return false;
            }
        }
        return up;
    }
Actor Biomüll1:
public class Biomüll1 extends x
{

    public Biomüll1()
    {
        moveReset();
    }

    public void act() 
    {
        move1();
        if(down()&&move1)
        {
            move3();
            if(up()&&move3)
            {
                move6();
                points += right;
                ((Muelltrennung)getWorld()).prepare();
                moveReset();
                getWorld().removeObject(this);
            }
            else
            {
                points += wrong;
                ((Muelltrennung)getWorld()).prepare();
                moveReset();
                getWorld().removeObject(this);
            }
        }
        else
        {
            points += wrong;
            ((Muelltrennung)getWorld()).prepare();
            moveReset();
            getWorld().removeObject(this);
        }
    }
}
Cool Cool

2020/2/15

#
Oh There is definitely something wrong abot that code because the timer doesn´t count while I´m not pressing any button.
danpost danpost

2020/2/15

#
Maybe you should be using if instead of while in your move# methods. It does not make sense to use while anyway as currently your move1 is equivalent to the following:
public void move1()
{
    if (getX() < 12 && getY() == 28) setLocation(12, 28);
    move1 = true;
}
I find it difficult to understand what all these move# methods are doing (as a whole). It seems as though you are trying to program the entire sequence of moves into one act step. The act method needs to be programmed with the idea of "what should I do at this instance of time", not "what should my overall behavior be".
danpost danpost

2020/2/15

#
Cool wrote...
Oh There is definitely something wrong abot that code because the timer doesn´t count while I´m not pressing any button.
That is due to the while loops in your x class methods. A while loop will not allow anything to happen, except for what it does, during its execution. No other code can execute until the loop condition is no longer met. So, as long as no key is pressed, execution is stuck in the loop.
Cool Cool

2020/2/15

#
danpost wrote...
Maybe you should be using if instead of while in your move# methods. It does not make sense to use while anyway as currently your move1 is equivalent to the following:
public void move1()
{
    if (getX() < 12 && getY() == 28) setLocation(12, 28);
    move1 = true;
}
I find it difficult to understand what all these move# methods are doing (as a whole). It seems as though you are trying to program the entire sequence of moves into one act step. The act method needs to be programmed with the idea of "what should I do at this instance of time", not "what should my overall behavior be".
This is what move# should do
Super_Hippo Super_Hippo

2020/2/15

#
The way I would do it would be the following: Have a two-dimensional target variable. Save the x and y coordinate of the current target. The target is always the next intersection. So if the waste object has a target, it moves in the direction of the target. If it reached the target, set the next target based on the type of waste it is* so it continues moving to the correct destination. *Edit: Re-reading that it is a game and not a simulation, make it set the new target based on the keypress which was done since the last intersection.
Cool Cool

2020/2/16

#
Super_Hippo wrote...
The way I would do it would be the following: Have a two-dimensional target variable. Save the x and y coordinate of the current target. The target is always the next intersection. So if the waste object has a target, it moves in the direction of the target. If it reached the target, set the next target based on the type of waste it is* so it continues moving to the correct destination. *Edit: Re-reading that it is a game and not a simulation, make it set the new target based on the keypress which was done since the last intersection.
I didn't really understand what you meant by that but I changed the code like this:
public void move1()
    {
        xz=12;
        yz=32;
        x=getX();
        y=getY();
        while(x != xz && y == yz)
        {
            turnTowards(xz,yz);
            move(1);
            x=getX();
            y=getY();
        }
        move1 = true;
    }
Is that what you meant?
Super_Hippo Super_Hippo

2020/2/16

#
Well, turning towards the target and moving in the direction is kinda correct. However, you don’t want to have that in a while loop or it will move there instantly.
danpost danpost

2020/2/16

#
Here is a simple Player class that has a behavior similar to what you want. Maybe it can point you in the right direction.
import greenfoot.*;

public class Player extends Actor
{
    private int[] xs = { 200, 400, 560, 700 };
    private int d;
    private int step;
    
    public void act()
    {
        if (getX() < xs[step]) setLocation(getX()+4, getY()+d);
        else
        {
            int dy = 0;
            if (Greenfoot.isKeyDown("up")) dy--;
            if (Greenfoot.isKeyDown("down")) dy++;
            if (dy != 0)
            {
                d += dy;
                step++;
                if (step == xs.length) getWorld().removeObject(this);
            }
        }
    }
}
Cool Cool

2020/2/16

#
I uploaded my Scenario here... It Works!!! Tank you all so much for your help! <3
You need to login to post a reply.