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 This is where I want it to scroll left and right depending on the keypress
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();
}
}
}
