我正在尝试制作在线游戏。服务器发送周围"块的数据"到客户端,然后保存块,并在绘制时检查这些块的已保存数据并绘制它们。因此,绘制的数据将保存在客户端的Array上。即使数据保存在该阵列上,如果我使用ip连接到服务器,图形似乎闪烁,但当连接作为本地主机时,图形就好了。我也尝试过使用双缓冲(当使用localhost运行时它也可以正常工作而没有双缓冲区)但是当使用ip时它仍然闪烁(即使我使用双缓冲(不太确定它是否正常工作)虽然..))所以我要问的是:
1)为什么在使用ip而不是localhost时它会闪烁? (它不能因为插座上的流量过大而导致)
2)我怎样才能做更好的双缓冲(我已经使用了youtube和互联网的多个指南,如this) 到目前为止,我使用过的导游都没有帮助避免闪烁..
绘图方法:
public class Area implements Serializable{
/**
*
*/
private static final long serialVersionUID = -7079895937774492131L;
private Tile[][] tiles;
private Object[][] objects;
private int x, y;
private static Chunk[] chunks;
//public static Area currentArea;
private static Area[][] areasave = new Area[512][512];
public Area(Tile[][] tiles, Object[][] objects, int x, int y){
this.tiles = tiles;
this.objects = objects;
this.x = x;
this.y = y;
}
public Area(Tile[][] tiles, Object[][] objects){
this.tiles = tiles;
this.objects = objects;
}
public static Tile[][] getTiles(){
return tiles;
}
public static Object[][] getObjects(){
return objects;
}
public static boolean addArea(Area area){
areasave[area.x][area.y] = area;
return true;
}
public static void Draw(Graphics g) {
if(chunks != null){
for(int i = 0; i < chunks.length; i++){
if(areasave[chunks[i].getX()][chunks[i].getY()] != null){
Tile[][] tiles = getTiles(areasave[chunks[i].getX()][chunks[i].getY()]);
Object[][] objects = getObjects(areasave[chunks[i].getX()][chunks[i].getY()]);
g.setColor(new Color(255, 255, 255));
if(tiles != null){
for(int y = 0; y < tiles.length; y++){
for(int x = 0; x < tiles[0].length; x++){
if(tiles[x][y] != null){
if(new File(Frame.dpath +"TileImages/" +Tile.getID(tiles[x][y]) +".png").exists()){ //if we have tile for that ID
g.drawImage(tileImages[Integer.parseInt(Tile.getID(tiles[x][y]))], ((Integer.parseInt(Tile.getX(tiles[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Player.y-Integer.parseInt(Tile.getY(tiles[x][y])))+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
}
else{
g.drawImage(tileImages[0], ((Integer.parseInt(Tile.getX(tiles[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Integer.parseInt(Tile.getY(tiles[x][y]))-Player.y)+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
}
}
}
}
}
if(objects != null){
for(int y = 0; y < objects.length; y++){
for(int x = 0; x < objects[0].length; x++){
if(objects[x][y] != null){
if(new File(Frame.dpath +"ObjectImages/" +Object.getID(objects[x][y]) +".png").exists()){ //if we have object for that ID
g.drawImage(objectImages[Integer.parseInt(Object.getID(objects[x][y]))], ((Integer.parseInt(Object.getX(objects[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Player.y-Integer.parseInt(Object.getY(objects[x][y])))+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
//System.out.println(Integer.parseInt(Object.getID(objects[x][y])) +", " +Object.getX(objects[x][y]) +"-" +Player.x +"," +((Player.y-Integer.parseInt(Object.getY(objects[x][y])))+GameLayout.tilesHeight/2)*(Screen.Height/GameLayout.tilesHeight));
}
else{
g.drawImage(objectImages[0], ((Integer.parseInt(Object.getX(objects[x][y]))-Player.x)+GameLayout.tilesWidth/2)*(Frame.Width/GameLayout.tilesWidth), ((Integer.parseInt(Object.getY(objects[x][y]))-Player.y)+GameLayout.tilesHeight/2)*(Frame.Height/GameLayout.tilesHeight), Frame.Width/GameLayout.tilesWidth, Frame.Height/GameLayout.tilesHeight, null);
}
}
}
}
}
}
}
}
}
上面的Draw方法确实闪烁,即使我尝试使用bufferStrategy(我不太确定,如果我做得还好......)我非常乐意提供更多代码/描述更多等,如果需要的话