好吧,非常抱歉,如果這是一個noob問題,但這是第一次製作遊戲,我只想知道如何在gameloop中調用,我已經在我的Keylistener類中創建了它,但是每當我運行它時,它都會因run()方法中的infinate while循環而凍結,但我也想在遊戲啓動時調用它,以便可以看到整個遊戲中的蜱/ fps,這裏是我的代碼,如果你們需要任何其他類,請說謝謝:)更容易我只是要顯示keylistener類,因此它不是很多代碼來排序,只是爲了讓你知道我製作網格的方式是使用名爲WizardCells的JLabel數組,所以你知道,我使用了圖標我沒有使用BufferedImage的圖標,無論如何,我認爲這就是你需要知道的全部內容,謝謝: ) BTW timePassed是三角洲我只是不喜歡這個名字三角洲哈哈如何在整個程序中調用我的gameloop以及在tick中放什麼()
@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener, Runnable{
public static boolean isRunning = true;
static Thread thread = new Thread();
protected synchronized static void start(){
if(isRunning)
return;
isRunning = true;
thread = new Thread();
thread.start();
}
protected synchronized static void stop(){
if(!isRunning)
return;
isRunning = false;
try{
thread.join();
}
catch(InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
public void run() {
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000/amountOfTicks;
double timePassed = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(isRunning){
long currentTime = System.nanoTime();
timePassed += (currentTime - lastTime)/ ns;
lastTime = currentTime;
if(timePassed >= 1){
tick();
updates++;
timePassed--;
}
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println(updates + " ticks, fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
private void tick(){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getGoodGuy().gety() +1)].setIcon(getBlueSpell().getIcon());
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
while(getBlueSpell().gety() != 19){
run();
}
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
while(getBlueSpell().gety() != 19){
run();
}
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
}
}
@Override
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_S){
if(getGoodGuy().getx() != 19){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_D){
if(getGoodGuy().gety() != 9){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_A){
if(getGoodGuy().gety() != 0){
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}
else if(key == KeyEvent.VK_SPACE){
while(getBlueSpell().gety() != 19){
run();
}
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
}
}
}
好的,謝謝你的建議,你知道我可以做什麼,而無需執行mvc方法嗎?我只是基本上試圖射擊子彈,我想避免mvc的原因是因爲我不得不重寫我的所有代碼,並且我花了一個月的時間才把這個遠處哈哈,如果這是我能做的唯一的事情我會做,我只是看到如果你知道任何其他方式來做到這一點,謝謝:) – user3471511