1

这听起来与我的其他帖子相似,但相信我,它不一样。所以我决定听取一些人关于如何滚动数组列表的建议。我有一个接受 Block 类的数组列表。这是块类:

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;
}
}

我会在我的主类的主更新方法中调用更新方法。喜欢:

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

然后向左/向右移动所有块我会使用:

    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;
    }
}

我无法获得屏幕截图,因为它捕获的速度不够快。似乎是延迟导致了它或其他什么。有什么更顺畅的方法来做到这一点?

顺便说一句,我的图像是 32x32

我的渲染代码: 用户请求:

public Main(){

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

游戏循环(不要担心它在线程中)

while(true){

    render();

}

渲染方法:

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

}
4

0 回答 0