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:
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:
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
/*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); } } }