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

2017/12/8

Greenfoot low FPS and Speed

SamuelIS SamuelIS

2017/12/8

#
Since updating a new method in world my scenario is really slow. While running the game the world have to add Object in a delay to the world. Not only the actors lag, the scenario lags because my counting var counts slow... Someone an idea for help? If you need the scenario code and not only the world code, ask me for Below the world code
import greenfoot.*;
public class Spiel extends World
{
    Raumschiff raumschiff;
    GreenfootSound backgroundSound = new GreenfootSound("Fly.mp3");
    private int ad=0;
    private int ay;
    private int at=0;
    public Spiel()
    {
        super(900, 900, 1);
        backgroundSound.playLoop();
        GreenfootImage bg = new GreenfootImage("bgspiel.png");
        raumschiff=new Raumschiff();
        this.addObject(raumschiff, 450, 450);
    }  
    public void act()
    {
        Gegnerspawnen();
    }
    public void Gegnerspawnen()
    {
        ay=Greenfoot.getRandomNumber(4);
           Enemy enemy=new Enemy(1);
           Tank tank=new Tank();
           if(ad==40){
                   if(ay==0){
            this.addObject(enemy, Greenfoot.getRandomNumber(900), 0);
            ad=0;
            }
                   if(ay==1){
            this.addObject(enemy, Greenfoot.getRandomNumber(900), 900);
            ad=0;
            }
                   if(ay==2){
            this.addObject(enemy, 0, Greenfoot.getRandomNumber(900));
            ad=0;
            }
                   if(ay==3){
            this.addObject(enemy, 900, Greenfoot.getRandomNumber(900));
            ad=0;
            }
           }
           if(at==200)
           {            
               if(ay==0){
            this.addObject(tank, Greenfoot.getRandomNumber(900), 0);
            at=0;
            }
                   if(ay==1){
            this.addObject(tank, Greenfoot.getRandomNumber(900), 900);
            at=0;
            }
                   if(ay==2){
            this.addObject(tank, 0, Greenfoot.getRandomNumber(900));
            at=0;
            }
                   if(ay==3){
            this.addObject(tank, 900, Greenfoot.getRandomNumber(900));
            at=0;
            }
           }
           ad++;
           at++;
    }
}
Super_Hippo Super_Hippo

2017/12/8

#
First, you can simplify your Gegnerspawnen method (which should start with a lowercase letter, for example gegnerSpawnen or spawnGegner).
if (++ad == 40)
{
    ad = 0;
    switch (Greenfoot.getRandomNumber(4))
    {
        case 0: addObject(new Enemy(1), Greenfoot.getRandomNumber(900), 0); break;
        case 1: addObject(new Enemy(1), Greenfoot.getRandomNumber(900), 900); break;
        case 2: addObject(new Enemy(1), 0, Greenfoot.getRandomNumber(900)); break;
        case 3: addObject(new Enemy(1), 900, Greenfoot.getRandomNumber(900)); break;
    }
}
if (++at == 200)
{
    at = 0;
    switch (Greenfoot.getRandomNumber(4))
    {
        case 0: addObject(new Tank(), Greenfoot.getRandomNumber(900), 0); break;
        case 1: addObject(new Tank(), Greenfoot.getRandomNumber(900), 900); break;
        case 2: addObject(new Tank(), 0, Greenfoot.getRandomNumber(900)); break;
        case 3: addObject(new Tank(), 900, Greenfoot.getRandomNumber(900)); break;
    }
}
This shouldn't really have an effect though. If it is still lagging after this change, show the constructor of Enemy and Tank.
SamuelIS SamuelIS

2017/12/8

#
I'll try. thx u
danpost danpost

2017/12/8

#
I do not see anything that would seriously cause any lagging. The suggestion Hippo gave to show the constructors of other classes might help. However, showing entire class codes would be more helpful so as to leave no stone left unturned (if you know what I mean).
You need to login to post a reply.