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

2018/5/9

Scrolling World Image Not Appearing

Applez Applez

2018/5/9

#
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 :
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();
        }
    }      
}
Code For Moving Background/Road :
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); 
}
}
Thank you
danpost danpost

2018/5/9

#
Applez wrote...
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.
Line 24 in the Road class does not look right with getHeight in the x-coordinate expression. That is the first thing I had noticed, at least. You should probably think through exactly what that method should do.
You need to login to post a reply.