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

2013/12/5

Greenfoot drawImage

Bolino Bolino

2013/12/5

#
Sup folks. I want to make a game with a scrolling background. But generating is too hard for me, so I would like to take one big image. Width = 10000 and height 600. And using drawImage to get a scrolling background. It's for a "Copter"-based game. I approx need 15 rules of code. Could someone give me an example using drawImage? Thanks in advance!
Gevater_Tod4711 Gevater_Tod4711

2013/12/5

#
I'm not shure if this would work. A GreenfootImage with a width of 10000 and a height of 600 will take very much space in your RAM. And there is not so much of it which greenfoot can use. But you can try it. To make the background look like a scrolling background you just need to change the offset of the image. I think the height of your world is 600, right? In this case you would only need a offset for the x direction. You can draw the image like this:
//in your world subclass;
private int offset = 0;

private GreenfootImage bgImage; //initialse this reference with your background image;

public void redrawBackground() {
    drawImage(bgImage, offset, 0);
}

public void setOffset(int offset) {
    if (offset > 0 && offset < bgImage.getWidth() - getWidth()) {
        this.offset = offset;
    }
    redrawBackground();
}
}
If you use the setOffset method with the parameter 0 the left of the image is drawn and with the parameter 10000-worldWidth the right of the image is drawn.
Bolino Bolino

2013/12/5

#
Could u help me out here? I copied the code identically but I get a warning: Illegal start of expression on "private int offset = 0"
danpost danpost

2013/12/5

#
Please show the code you have (the entire class). You either have something misplaced or are missing brackets. Line 7 of the code Gevater_Tod4711 gave should probably be:
drawImage(bgImage, -offset, 0);
If the range of 'offset' is to be between zero and image width minus world width, then the drawing of the image needs to start at or before the beginning of the world width.
Bolino Bolino

2013/12/7

#
Thank for your reply, this is my current code. Like I said, I need a copter-based game. But I generating background is far to hard for me. So I would like to create a background from 10000px width and 600px height. And while the copter is moving I need the 'current' part of the picture to shift every time if you know what I am tryin to say? :)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
            //in your world subclass;  
            private int offset = 0;  
            private GreenfootImage Background; //initialse this reference with your background image;  
      
            public void redrawBackground() 
            {  
            drawImage(Background, offset, 0);  
            
            }     
          
            public void setOffset(int offset) {  
            if (offset > 0 && offset < Background.getWidth() - getWidth()) {  
                this.offset = offset;  
            }  
            redrawBackground();  
            }  
    }  
    }
}
Note: " Background " is name of my World Subclass
You need to login to post a reply.