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

2015/1/8

Scenario is lagging

berdie berdie

2015/1/8

#
public class game3 extends World
{
    private int spawnX = 0;
    private int spawnX2 = 0;
    private int spawnX3 = 0;
    private int spawnCounter = 0;
    private int spawnTreasure = 0;
    private Counter theCounter;
    private static final String bgImageName = "watergoed.jpg";
    private static final int scrollSpeed = 3;
    private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
    private GreenfootImage bgImage, bgBase;
    private int scrollPosition = 0;
    private int timer = 3600;
    TimerText timerText = new TimerText();
    /**
     * Dit is de constructor voor game3. 
     * We pakken ook de background image hier zodat we in een andere methode 
     * de background kunnen tekenen zodat het lijkt alsof die naar beneden scrolled.
     */

    public game3()
    {    
        super(900, 900, 1); 
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(getWidth(), picHeight);
        bgBase.drawImage(bgImage, 0, 0);
        prepare();
        addObject(timerText, 100, 15); //wherever
        timerText.setText("Time left: " + (timer/60));
    }
    /**
     * We returnen de waarde van theCounter hier.
     */
    public Counter getCounter()
    {
        return theCounter;
    }
    /**
     * In de act methode tekenen we de background image voor de scroll effect. 
     * We hebben ook de methode addObjects gebruikt om random objecten te spawnen in de wereld.
     * Ook de werking van de timer staat erin.
     */
    public void act()
    {
        addObjects();
        if (timer%60==0) timerText.setText("Time left: " + (timer/60));
         timerFunc();
        scrollPosition += scrollSpeed;
        while(scrollSpeed > 0 && scrollPosition > picHeight) scrollPosition -= picHeight;
        while(scrollSpeed < 0 && scrollPosition < 0) scrollPosition += picHeight;
        paint(scrollPosition);
    }
}
       
        
    /**
     * Hier word de background image echt getekend.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(bgBase,0, position);
        bg.drawImage(bgImage, 0, position-(int)Math.signum(scrollSpeed)*picHeight);
    }
There is some code which i didn't post, but for some reason i keep getting some lag spikes with the scrolling background(tested it) and i can't seem to fix it, is it the refence to the image because he keeps getting the image from the image map? Anyone any suggestions?
danpost danpost

2015/1/9

#
A 900 by 900 background is a large area to cover with an image; and having to paint an image that size every act cycle will most undoubtedly be a cause of lagging. You could scale down the size of the project. At half size, 450 by 450, you would be reducing the workload by 75 percent. That would certainly help in reducing the lag. Most computers do not have screens of that height (900) anyway -- most everyone would have to either zoom out to reduce the size to fit their screens or be tied down to using the scroll bars (which may not work too well on the site); there is little on the downside of doing this, either.
You need to login to post a reply.