Hello! I've made a scrolling world which adds 2 images and makes them go down when the up arrow key is pressed, both the images move down but they don't follow reappear after they have gone out of sight... It's like they go down and i can never see the actor again.
Code for World :
Code For Moving Background/Road :
Thank you
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Road here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Road extends World
{
private Road2 img0,img1;
/**
* Constructor for objects of class Road.
*
*/
public Road()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(500,800,1,false);
img0 = new Road2();
addObject( img0, getWidth()/2, getHeight()/2);
img1 = new Road2();
addObject( img1, getWidth()/2, getHeight()+8);
}
public void act()
{
if (Greenfoot.isKeyDown("up")){
img0.scroll();
img1.scroll();
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Road2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Road2 extends Actor
{
/**
* Act - do whatever the Road2 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void scroll()
{
if (getY()< - getImage().getHeight()/2){
setLocation( getWorld().getHeight(), getY());
}
setLocation(getX(), getY()+5);
}
}
