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

2021/6/3

Jitter lag problem with background

Suavessence Suavessence

2021/6/3

#
Hello, my scenario has a visible issue in which the scrolling background appears to jitter or lag every second or so. Any idea how to fix this? Thanks. (The background is made of 2 png images that scroll down below the bottom of the screen, and are then re-positioned back at the top. At this point I have tried reducing the scenario to include only the Stars actor yet the issue persists.)
import greenfoot.*;

public class Space extends World  //space. the final frontier.
{
    private int time;
    private int score;
    private int initialDelay;
    Stars stars1 = new Stars();
    Stars stars2 = new Stars();
    
    public Space()
    {    
        super(480, 600, 1, false); 
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        setPaintOrder(ButtonMain.class, ScoreBoard.class, Hud.class, Clouds.class, SlowCloud.class, 
            AsteroidTwo.class, AsteroidOne.class, Explosion.class, Ship.class, StarsSmall.class, Stars.class);
        score = 0;
        time = 0;
        initialDelay = 125;
    }

    public void act()
    {
        makeStars();
        countTime();
    }

    private void makeStars() //this is a vertically scrolling background made of 2 images
    {
       addObject(stars1, 240, -300); //png image 480 X 600 (same size as game screen)
       addObject(stars2, 240, 300); //png image 480 X 600 (same size as game screen)

       if(stars1.getY() >= 900)
       {
           stars1.setLocation(240, -300); //once the image is out of site below the bottom border, move it above the top border
       }
       if(stars2.getY() >= 900)
       {
           stars2.setLocation(240, -300); //once the image is out of site below the bottom border, move it above the top border
       }
    }

    private void countTime()
    {
        if (lives != 0)
        {
            time++;
        }
    }

    public void storeHighScore() //Game over. Store the player score in the high score table if possible.
    {
        if(UserInfo.isStorageAvailable()) {
            UserInfo myData = UserInfo.getMyInfo();
            if (myData != null) {
                if (finalScore() > myData.getScore()) {
                    myData.setScore (finalScore());
                    myData.store();  // write back to server
                }
            }
        }
    }
}
import greenfoot.*;

public class Stars extends Actor
{
    private int speed;

    protected void addedToWorld(World world)
    {
        Space space = (Space) world;
        speed = 6;
    }
    
    public void act() 
    {
        setLocation(getX(), getY()+speed);
    }  
}
danpost danpost

2021/6/3

#
Lines 32 and 33 should be in the constructor of Space class. Use 200 for x-coordinate in placing Stars objects in world. The rest of the makeStars method should be done in the act method of Stars class. Reposition stars by relative position (don't use literal value for y-coordinate when re-posiitioning; use "getY()-1200").
Suavessence Suavessence

2021/6/3

#
Thanks danpost, that certainly cleans things up. Unfortunately there is still a jitter.
danpost danpost

2021/6/3

#
Suavessence wrote...
Thanks danpost, that certainly cleans things up. Unfortunately there is still a jitter.
Show updated codes.
Suavessence Suavessence

2021/6/3

#
import greenfoot.*;
 
public class Space extends World  //space. the final frontier.
{
    private int time;
    private int score;
    private int initialDelay;
    Stars stars1 = new Stars();
    Stars stars2 = new Stars();
     
    public Space()
    {    
        super(480, 600, 1, false); 
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        setPaintOrder(ButtonMain.class, ScoreBoard.class, Hud.class, Clouds.class, SlowCloud.class, 
            AsteroidTwo.class, AsteroidOne.class, Explosion.class, Ship.class, StarsSmall.class, Stars.class);
        addObject(stars1, 200, -300); //png image 480 X 600 (same size as game screen)
        addObject(stars2, 200, 300); //png image 480 X 600 (same size as game screen)
        score = 0;
        time = 0;
        initialDelay = 125;
    }
 
    public void act()
    {
        countTime();
    }
 
    private void countTime()
    {
        if (lives != 0)
        {
            time++;
        }
    }
 
    public void storeHighScore() //Game over. Store the player score in the high score table if possible.
    {
        if(UserInfo.isStorageAvailable()) {
            UserInfo myData = UserInfo.getMyInfo();
            if (myData != null) {
                if (finalScore() > myData.getScore()) {
                    myData.setScore (finalScore());
                    myData.store();  // write back to server
                }
            }
        }
    }
}
import greenfoot.*;
 
public class Stars extends Actor
{
    private int speed;
 
    protected void addedToWorld(World world)
    {
        Space space = (Space) world;
        speed = 6;
    }
     
    public void act() 
    {
        setLocation(getX(), getY()+speed);
	    if(getY() == 900)
        {
           setLocation(200, getY() - 1200); //once the image is out of site below the bottom border, move it above the top border
        }
    }
}       
danpost danpost

2021/6/3

#
I do not see any reason why there might be a jitter.
Suavessence Suavessence

2021/6/3

#
Alright, thanks for your time.
danpost danpost

2021/6/3

#
Stars class can be simplified to this:
import greenfoot.*;
  
public class Stars extends Actor
{
    private int speed = 6;
    
    public void act() 
    {
        setLocation(getX(), (getY()+speed+300)%1200-300);
    }
}
You need to login to post a reply.