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

2021/4/16

How can I make my background scroll left and right?

Samuaelk Samuaelk

2021/4/16

#
I'm currently having 2 issues. 1 is I want my background to scroll right when the right key is pressed, and left when the left key is pressed. I want both sides to be infinite. The second problem I'm having is placing platforms outside the world since my world is only 1000 px wide. Here is all my code Game world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 1 ;
    private int jumpStrenght = -6;
    private Background img0, img1, img2;
    /**
     * Constructor for objects of class Game.
     * 
     */

    public Game()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1,false); 
        // Add New Hero//
        
        img0 =new Background();
        addObject( img0,getWidth()/2, getHeight()/2 );

        img1 = new Background();
        addObject( img1, getWidth() + getWidth()/2, getHeight()/2);
        
        img2 = new Background();
        addObject( img2, getWidth() - getWidth()/2, getHeight()/2);
        Hero hero = new Hero();
        addObject(Globals.h, 50, 400);
        addObject(new Long(), 189, 650);
        addObject(new Hump(), 581, 572);
        addObject(new Long(), 500, 476);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        
}

    public void act()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            img0.scroll();
            img1.scroll();
            img2.scroll();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            img0.scroll();
            img1.scroll();
            img2.scroll();
            
        }
    }
}
This is where I want it to scroll left and right depending on the keypress
RcCookie RcCookie

2021/4/16

#
Spawning actors outside of your world should not be a problem, because you have already disabled world bounds in your constructor. Just pass the appropriate coordinates, for example for x -1 or the world witdth or more.
RcCookie RcCookie

2021/4/16

#
Concerning the scrolling: you can actually get away with only 2 background objects like this:
// In world constructor
addObject(new Background(), 0, getHeight() / 2);
addObject(new Background(), getWidth(), getHeight() / 2);

// In world
@Override
public void act() {
    if(Greenfoot.isKeyDown("right")) scroll(SCROLL_SPEED);
    else if(Greenfoot.isKeyDown("left")) scroll(-SCROLL_SPEED);
}

private void scroll(int speed) {
    for(Background b : getObjects(Background.class)) b.move(speed);
}

// In Background class
@Override
public void setLocation(int x, int y) {
    while(x < -getWorld().getWidth() / 2) x += getWorld().getWidth() * 2;
    while(x >= (int)(getWorld().getWidth() * 1.5)) x -= getWorld().getWidth() * 2;
    super.setLocation(x, y);
}
The world just moves the background left and right, and the background always checks weather it is within bounds. If it is not it will adjust itself and switch sides. And because the code uses "getObjects()" we don't need to save the background objects into variables / an array.
Samuaelk Samuaelk

2021/4/17

#
Thanks!
You need to login to post a reply.