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

2014/4/27

Strange lag?

Unicus Unicus

2014/4/27

#
I tried to use up danpost's GameOver code, but in my project, it gives me really bag lags, and after awhile, I run out of memory. Here is his code:
import greenfoot.*;
import java.awt.Color;

public class GameOver extends Actor
{
    Color[] colors = { Color.ORANGE, Color.RED };
    int colorNum = 0;
    int counter = 1;

    public void act()
    {
        if (--counter == 0)
        {
            colorNum = ++colorNum % 2;
            updateImage();
            counter = 50;
        }
    }
    
    public void updateImage()
    {
        setImage(new GreenfootImage("GAME\nOVER", 96, colors[colorNum], new Color(0, 0, 0, 0)));
    }
}
and here is mine:
import greenfoot.*;
import java.awt.Color;

public class GameOver extends Text
{
    Color[] colors = { Color.ORANGE, Color.RED };
    private int colorNum = 0;
    private static int contador = 50;

    public void act()
    {
        if (contador == 50)
        {
            if (Garage2.health2 <= 0)
                {
                    updateImage1();
                }
                else if (Garage1.health1 <= 0)
                    {
                        updateImage2();
                    }
            colorNum = ++colorNum % 2;
            contador = 0;
        }
        else {contador++;}
    }
        
    public void updateImage1()
    {
        setImage(new GreenfootImage("GAME OVER\nPlayer 1 won!", 96, colors[colorNum], new Color(0, 0, 0, 0)));
    }
    public void updateImage2()
    {
        setImage(new GreenfootImage("GAME OVER\nPlayer 2 won!", 96, colors[colorNum], new Color(0, 0, 0, 0)));
    }
}
What am I missing here?
danpost danpost

2014/4/27

#
I plugged your GameOver class into a blank scenario and tested it (only changed what it extends to 'Actor'). Had no problems with it. Your lag must be coming from some other code. Try placing the following line before your line that creates the 'GameOver' object and adds it to the world:
getWorld().removeObjects(getWorld().getObjects(null));
The above line is if you create the GameOver object from an Actor subclass. From the world, use
removeObjects(getObjects(null));
If after adding the line, the lag is no longer there, then some other actor in your world was causing the lag problem. BTW, create the GameOver object before the line and add it to the world after the line.
Unicus Unicus

2014/4/27

#
Thanks, I'll check it out!
danpost danpost

2014/4/27

#
danpost wrote...
BTW, create the GameOver object before the line and add it to the world after the line.
I said this wrong. Before the line you need to set a local variable with the world -- like this:
World world  = getWorld();
world.removeObjects(world.getObjects(null));
world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
Unicus Unicus

2014/4/27

#
Did it like this:
    public void healthChecker()
    {
        if (Garage1.getHealth1() <= 0 || Garage2.getHealth2() <= 0)
        {
            if (gameover)
            {
                showGameOver();
            }
            else
            {
                removeObjects(getObjects(null));
                gameover = true;
            }
        }
    }
Unicus Unicus

2014/4/27

#
(in the world, that is)
danpost danpost

2014/4/27

#
Unicus wrote...
Did it like this:
    public void healthChecker()
    {
        if (Garage1.getHealth1() <= 0 || Garage2.getHealth2() <= 0)
        {
            if (gameover)
            {
                showGameOver();
            }
            else
            {
                removeObjects(getObjects(null));
                gameover = true;
            }
        }
    }
This is the source of your lag and your out of memory error. Once you set gameover to true the first part will continually run 'showGameOver', which I presume creates a GameOver object one per act cycle. I will not take long for memory to run out at that rate. Change your 'healthChecker' method to this:
public void healthChecker()
{
    if ((Garage1.getHealth1() <= 0 || Garage2.getHealth2() <= 0) && !gameover)
    {
        gameover = true;
        removeObjects(getObjects(null);
        showGameOver();
    }
}
If this method is the only place you use the 'gameover' field, then you probably do not need it at all -- just replace '!gameover' in the 'if' condition to 'getObjects(GameOver.class).isEmpty()'.
Unicus Unicus

2014/4/27

#
The lag is gone now; If I change the code to the second one, it keeps removing and adding the object gameover, so I guess I'll leave it as it is. It no longer fills my memory, so it's ok. I just published the game (beta version still), so if you want to take a look, you can find it here: http://www.greenfoot.org/scenarios/11355
You need to login to post a reply.