After restarting the level a few times (from character dying) i get Heap space memory error. Too be fair i scale the background in the world superclass ,but are there any way to fix this ? I scale the image only once in the constructor.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
/**
* Write a description of class LumeScrolling here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LumeScrolling extends World
{
public static final int lungime = 1500;
public static final int inaltime = 600;
Actor actorPrincipal;
Scroller scroller;
private boolean pause = false,unpause = true,afisat = false,ok = false;
boolean play = true;
private static boolean okv0 = true,okv1 = true,okv2 = true;
private static String r;
private static int s=Rez();
private static int shouldAddB =600;
private boolean afisatB=false;
Pauza pauza = new Pauza(1500,600);
Viata0 viata0 = new Viata0(41, 41);
Viata1 viata1 = new Viata1(41, 41);
Viata2 viata2 = new Viata2(41, 41);
public LumeScrolling()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(lungime*Rez()/3,inaltime*Rez()/3, 1,false);
GreenfootImage bg = new GreenfootImage("cyberpunk-street-dublu.png");
setBackground(bg);
bg.scale(lungime*s/3*2,inaltime*s/3);
int lungBg = bg.getWidth();
int inalBg = bg.getHeight();
setPaintOrder(Pauza.class,Viata0.class,Viata1.class,Viata2.class,Tutoriale.class,Intrebari.class,Elev.class,Platforma.class,Cutie.class,PC.class);
actorPrincipal = new Elev();
scroller = new Scroller(this, bg,2*lungime*s/3,inaltime*s/3);
addObject(actorPrincipal ,100,100);
Greenfoot.setSpeed(49);
addObject(viata0,lungime*s/3-(59*s/3),37*s/3);
addObject(viata1,lungime*s/3-(59*s/3)-(50*s/3),37*s/3);
addObject(viata2,lungime*s/3-(59*s/3)-2*(50*s/3),37*s/3);
viata0.setOk(okv0);
viata1.setOk(okv1);
viata2.setOk(okv2);
}
public void act()
{
// if(actorPrincipal!=null)
scroll();
String kInput = Greenfoot.getKey();
okv0 = viata0.getOk();
okv1 = viata1.getOk();
okv2 = viata2.getOk();
if(!okv2)
play = false;
if(unpause)
{
CheckPause(kInput);
ok = true;
}
else if(pause)
{
CheckUnpause(kInput);
ok = true;
}
if(shouldAddB==600&&!afisatB)
{
AddBoostere();
afisatB=true;
}
else
{
shouldAddB++;
afisatB=false;
}
}
public void scroll()
{
int x = actorPrincipal.getX()-lungime/2;
int y = actorPrincipal.getY()-inaltime/2;
scroller.scroll(x,y);
}
public void CheckPause(String key)
{
if(key != null)
{
if("p".equals(key))
{
if(!afisat)
{
addObject(pauza,1500/2*s/3,600/2*s/3);
afisat = true;
}
play = false;
pause = true;
unpause = false;
}
}
}
public void CheckUnpause(String key)
{
if(key != null)
{
if("p".equals(key))
{
removeObject(pauza);
afisat = false;
play = true;
pause = false;
unpause = true;
}
}
}
public void AddBoostere()
{}
public static void setShouldAddB(int x)
{
shouldAddB=x;
}
public void NivelUrmator()
{
}
public void RestartNivel()
{
}
public static void setViata0(boolean x)
{
okv0=x;
}
public static void setViata1(boolean x)
{
okv1=x;
}
public static void setViata2(boolean x)
{
okv2=x;
}
public static boolean getViata0()
{
return okv0;
}
public static boolean getViata1()
{
return okv1;
}
public static boolean getViata2()
{
return okv1;
}
public static int Rez()
{
try{
FileReader fr = new FileReader("Rezolutie.txt");
BufferedReader br = new BufferedReader(fr);
r=br.readLine();
br.close();
s = Integer.parseInt(r);
}
catch(IOException e)
{
s=3;
}
return s;
}
public static int getRez()
{
return s;
}
}
bg.scale(lungime*s/3*2,inaltime*s/3);
public void RestartNivel()
{
Greenfoot.setWorld(new Nivel0());
}
public void NivelUrmator()
{
Greenfoot.setWorld(new Nivel1());
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Nivel0 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Nivel0 extends LumeScrolling
{
/**
* Constructor for objects of class Nivel0.
*
*/
private int s;
public Nivel0()
{
LumeScrolling.setShouldAddB(600);
prepare();
}
private void prepare()
{
s=LumeScrolling.getRez();
Platforma platforma = new Platforma(300);
addObject(platforma,376,522);
platforma.setLocation(398*s/3,535*s/3);
AI ai = new AI();
addObject(ai,659,525);
Platforma platforma2 = new Platforma(300);
addObject(platforma2,895,471);
platforma2.setLocation(903,426);
Cutie cutie = new Cutie(40, 40);
addObject(cutie,395,337);
platforma2.setLocation(745,514);
Cutie cutie2 = new Cutie(40, 40);
addObject(cutie2,349*2/3,343*2/3);
removeObject(cutie);
platforma2.setLocation(924,459);
platforma2.setLocation(924,448);
ViataPickup viatapickup = new ViataPickup(20, 20);
addObject(viatapickup,609*s/3,550*s/3);
Tutorial0 tutorial0 = new Tutorial0(30, 30);
addObject(tutorial0,403*s/3,498*s/3);
}
public void AddBoostere()
{
JumpBoost jumpboost = new JumpBoost(20, 20);
addObject(jumpboost,659*s/3-scroller.getScrolledX(),550*s/3);
}
public void RestartNivel()
{
Greenfoot.setWorld(new Nivel0());
}
public void NivelUrmator()
{
Greenfoot.setWorld(new Nivel1());
}
}