我正在使用内置的Java Graphics2D在java中制作基于tile的装备游戏。我的问题是,当玩家在地图上移动时,我的屏幕会闪烁。地图滚动(意味着玩家停留在屏幕的中心)。当地图移动时,它会闪烁。我已经使用过缓冲技术,但无济于事。我的主要代码如下:
package dev.thenamegenerator.MasterQuestAlpha.display;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import dev.thenamegenerator.MasterQuestAlpha.collision.WaterCollision;
import dev.thenamegenerator.MasterQuestAlpha.entities.MainPlayer;
import dev.thenamegenerator.MasterQuestAlpha.gfx.Assets;
import dev.thenamegenerator.MasterQuestAlpha.world.MovingMap;
import dev.thenamegenerator.MasterQuestAlpha.world.World;
import dev.thenamegenerator.MasterQuestAlpha.world.WorldRenderer;
public class Board extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
private MainPlayer player;
private MovingMap wholeMap;
private World map;
private WaterCollision watercollision;
private WorldRenderer render;
private int frames = 0;
private int ticks = 0;
Graphics2D g2d;
public Thread thread;
Image currentWorld;
private int width = 480;
private int height = 374;
public static boolean running = false;
public Board(){
addKeyListener(new TAdapter());
setFocusable(true);
}
public synchronized void start(){
if(running)return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void init(){
Assets.init();
map = new World();
player = new MainPlayer(224,128);
render = new WorldRenderer();
watercollision = new WaterCollision(map.getStartMap(), map.getRightMap());
watercollision.initCollision();
render.renderWorld(map.getStartMap(), map.getRightMap());
wholeMap = new MovingMap(render.getImage(), 0, 0);
}
public void tick() {
wholeMap.move();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
checkCollision();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g2d = (Graphics2D) g;
//CLEARS SCREEN
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);
//RENDER HERE
g2d.drawImage(wholeMap.getMapImage(), wholeMap.getX(), wholeMap.getY(), null);
g2d.drawImage(player.getImage(), 224, 128, this);
//END RENDER
g2d.dispose();
g.dispose();
bs.show();
}
public void run(){
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60D;
double ns = 1000000000D/amountOfTicks;
double delta = 0;
long lastTimer = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
delta += (now-lastTime)/ns;
lastTime = now;
if(delta>=1){
ticks++;
tick();
delta--;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
frames++;
render();
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer+=1000;
System.out.println("Frames: " + frames + ", ticks:" + ticks);
frames = 0;
ticks = 0;
}
}
stop();
}
public void checkCollision(){
if(player.getX() == (wholeMap.getX()-32)){
wholeMap.resetPreviousLocation();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
wholeMap.setDx(0);
wholeMap.setDy(0);
//System.out.println("Collision Detected!");
}
if(player.getX() == (wholeMap.getX()+wholeMap.getWidth()-32)){
wholeMap.resetPreviousLocation();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
wholeMap.setDx(0);
wholeMap.setDy(0);
//System.out.println("Collision Detected!");
}
if(player.getY() == (wholeMap.getY() - 32)){
wholeMap.resetPreviousLocation();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
wholeMap.setDx(0);
wholeMap.setDy(0);
//System.out.println("Collision Detected!");
}
if(player.getY() == (wholeMap.getY() + wholeMap.getHeight())){
wholeMap.resetPreviousLocation();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
wholeMap.setDx(0);
wholeMap.setDy(0);
//System.out.println("Collision Detected!");
}
if(watercollision.checkWaterCollisionX(player.getX(), player.getY())){
wholeMap.resetPreviousLocation();
watercollision.updateWaterBlocks(wholeMap.getDx(), wholeMap.getDy());
wholeMap.setDx(0);
wholeMap.setDy(0);
//System.out.println("VIKA");
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
wholeMap.keyReleased(e);
player.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
wholeMap.keyPressed(e);
player.keyPressed(e);
}
}
}
我该怎么办?