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

2013/6/4

Change background

Solimbum Solimbum

2013/6/4

#
Hello I have a little problem, I'm using this method http://www.greenfoot.org/scenarios/243 to make the background scroll and I don't know how to change the background that is being scrolled without making a new world. How can I do that?
Solimbum Solimbum

2013/6/4

#
I tried this but that doesn't work....
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot) 


public class ScrollWorld extends World 
{ 
    private static GreenfootImage bgImage1 = new GreenfootImage("space1.gif");
    private static GreenfootImage bgImage2 = new GreenfootImage("fond-vert1.jpg");
    
    private static GreenfootImage bgImage = new GreenfootImage("space1.gif");
    
    private static final int scrollSpeed = 1;
 
    private GreenfootImage scrollingImage;
    private int scrollPosition = 0;
    
    private int currentLevel = 1;
    public ScrollWorld()
    {
        super(800, 400, 1);
      
        GreenfootImage background = new GreenfootImage(800, 400);
        scrollingImage = getScrollingImage(800, 400);
        background.drawImage(scrollingImage, 0, 0);
        setBackground(background);
       
        addObject(new SpaceShip(), 350, 200);
    }

    public void act()
    {
        if(scrollSpeed > 0 && scrollPosition <= 0) {
            scrollPosition = getWidth();
        }
        if(scrollSpeed < 0 && scrollPosition >= getWidth()) {
            scrollPosition = 0;
        }
        scrollPosition -= scrollSpeed;
        paint(scrollPosition);
        if (checkForNextLevel() == true)
        {
            currentLevel = 2;
            addObject(new SpaceShip(), 350, 200);
        }
        
    }
    public boolean checkForNextLevel()
    {
        int numberOfSpaceShip = getObjects(SpaceShip.class ).size();

        
        if (numberOfSpaceShip == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /**
     * Paint scrolling image at given position and make sure the rest of
     * the background is also painted with the same image.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(scrollingImage, position, 0);
        bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0);
    }
 
    /**
     * Returns an image with the given dimensions.
     */
    private GreenfootImage getScrollingImage(int width, int height)
    {
        if (currentLevel == 2)
        {
            bgImage = bgImage2;
        }
        
        GreenfootImage image = new GreenfootImage(width, height);
        for(int x = 0; x < width; x += bgImage.getWidth()) {
            for(int y = 0; y < height; y += bgImage.getHeight()) {
                image.drawImage(bgImage, x, y);
            }
        }
        return image;
    } 
}
You need to login to post a reply.