Java Tiled-Image Scrolling

350 Views Asked by At

This is going to sound similar to my other post but trust me it's not the same. So I decided to take some peoples advice on how to scroll an arraylist.I have an arraylist that takes the Block class. Here is the Block Class:

public class Block {

BufferedImage img;
double x;
double y;
double dx=0,dy=0;
public Block(BufferedImage img , double x, double y){
    this.img = img;
    this.x = x;
    this.y = y;
}
public void Render(Graphics g){
    g.drawImage(img,(int)x,(int)y,null);
}
public void Update(){
    x+=dx*DATA.speed;
    y+=dy*DATA.speed;
}
}

I would call the update method in my main update method in my main class. like:

    private void Update()
{
    for (int i = 0; i < World.BLOCK_LIST.size(); i++){
        World.BLOCK_LIST.get(i).Update();
    }
}

Then to move all the blocks left/right I would use:

    public static void MoveLeft()
{
    for(int i = 0; i < World.BLOCK_LIST.size(); i++){
        World.BLOCK_LIST.get(i).dx = 2.5;
    }
}
public static void MoveRight()
{
    for(int i = 0; i < World.BLOCK_LIST.size(); i++){
        World.BLOCK_LIST.get(i).dx = -2.5;
    }
}

I couldn't get a screenshot because it wouldn't capture fast enough. It just seemed like it was lag that was causing it or something. What is a smoother way to do this?

Btw my image is 32x32

My Rendering code: Requested by a user:

public Main(){

     setSize(800,600);
     .................
     setVisible(true);
     createBufferStrategy(2);
}

Game loop(don't worry it's in a thread)

while(true){

    render();

}

Render method:

public void render(){

    BufferStrategy bs = getBufferStrategy;
    Graphics g = bs.getDrawGraphics();

    g.clearRect(0,0,screen_width,screen_height);
    //DrawOrWhatever(g);

    g.dispose();


    bs.show();
    Toolkit.getDefaultToolkit.sync();

}
0

There are 0 best solutions below