Scrolling Background with Slick2D

500 views Asked by At

Full source of what I'm trying to do can be found here

I am trying to make a background scroll as mentioned above, I have made progress to the point where it is infinite, but it seems to disappear mid way through. I am currently stuck and have no idea how to progress from this point.

You can demo this by cloning the git provided above into eclipse. It should work on the fly without any setup needed. The canvas size is 1000 by 720.

int bgend=-(1000-bg.getWidth());
public void update(int delta){
    x-=delta*0.2;
    nx-=delta*0.2;
    if(x<=bgend&&x>=-bgend+delta*0.2){nx=1000;}
    else if(nx<=bgend&&nx>=-bgend+delta*0.2){x=1000;

    }

This is the logic associated with the scrolling. Then these values are used to render the image,

public void render(Graphics g){

bg.draw(x, y, bg.getWidth(), 720);

bg.draw(nx, y, bg.getWidth(), 720);


}

All of this is also in the link provided. Thank you.

1

There are 1 answers

2
Rany Halabi On BEST ANSWER

Can i know the size of the picture and the size of the screen ?! I think maybe u need to "cut" the image in the render method depend on how much u scrolled down/up in the state.. let me be more cleary using bottons: lets assume that every "W" is 1 pixel up and every "S" is 1 pixel down in "y" axis ,the pic is 1000 pixels hight and the state is set to 500 pixel hight ,and lets assume that we start from the bottom of the that photo so we need to get higher ,so every W push we update a param "UP" in the update method and then in the render method we cut the photo using "IMAGE.getSubImage(XstartPoint,YstartPoint,hight,width)".. i hope that was useful to you mate.