1

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();

}
36redsoxfan
  • 250
  • 2
  • 5
  • 15
  • 1
    This may not have anything to do with it, but make sure your images are compatible with the screen's graphics context...`GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, transparancy)`. When I need to animate images, I tend to load them from a file and the copy them into a compatible context via the `Graphics.drawImage` method – MadProgrammer Aug 07 '12 at 04:53
  • nope that did nothing:( I appreciate you took time to answer though. Noone else did.. – 36redsoxfan Aug 07 '12 at 05:17
  • Are you able to produce a small runnable example (don't worry about the images) - that way we could run it locally and see if we can find anything – MadProgrammer Aug 07 '12 at 05:19
  • Here you go: https://www.dropbox.com/s/uqvadr6oj840jg3/Game%20Engine%202.zip – 36redsoxfan Aug 07 '12 at 06:09
  • Don't mind the images I have in res/images/ I know I'm a bad artist lol. There are some other images in there that I'm using later but don't delete them or it'll error out because I still have them initialized – 36redsoxfan Aug 07 '12 at 06:11
  • Are rendering to a backing buffer first or directly to the screen? ie - do you render each block to `BufferedImage` then paint the `BufferedImage` to screen, or do you paint the blocks directly?? – MadProgrammer Aug 07 '12 at 06:17
  • I would say directly? I'll post my rendering code so you can see – 36redsoxfan Aug 07 '12 at 06:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14975/discussion-between-36redsoxfan-and-madprogrammer) – 36redsoxfan Aug 07 '12 at 06:54

0 Answers0