2014-10-20 3 views
-2

Когда я устанавливаю его, я хочу, чтобы он менялся на другой экран, когда я нажимаю клавишу пробела, но когда я нажимаю любую клавишу, она автоматически закрывается. Если бы кто-нибудь мог помочь, это было бы здорово!Проблемы с экраном экрана игры

экран Класс:

package net.jacobmob.towerdefence; 

import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.JPanel; 

public class Screen extends JPanel implements Runnable{ 

Thread thread = new Thread(this); 

Frame frame; 
/** Account*/ 
User user; 

private int fps = 0; 

public int scene; 

public boolean running = false; 

public Screen (Frame frame) { 
    this.frame = frame; 

    this.frame.addKeyListener(new KeyHandler(this)); 

    thread.start(); 
} 

public void paintComponent(Graphics g) { 
    g.clearRect(0, 0, this.frame.getWidth(), this.frame.getHeight()); 

    //Background 
    if(scene == 0) { 
     g.setColor(Color.CYAN); 
     g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight()); 
    }else if (scene == 1) { 
     g.setColor(Color.GREEN); 
     g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight()); 

     //Grid 
     g.setColor(Color.GRAY); 
     for(int x =0; x < 22; x++) { 
      for(int y = 0; y < 13; y++) { 
      double width1 = getWidth() * 10000/1100;  
      double width2 = width1/10000; 
      double width3 = this.frame.getWidth()/width2; 
      double width = width3/22; 

      double height1 = getHeight() * 10000/700;  
      double height2 = height1/10000; 
      double height3 = this.frame.getHeight()/height2; 
      double height = height3/14; 

       g.drawRect(50 + (x * 50), 50 + (y * 50), (int) width, (int) height); 
      } 
     } 

    //Health + Moneys 
     g.drawRect(x, y, width, height); 

    }else{ 
     g.setColor(Color.WHITE); 
     g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight()); 
    } 


    //FPS AT THE BOTTOM 
g.drawString(fps + "", 10, 10); 
} 

//Only First Time 
public void loadGame() { 
    user = new User(this); 

    running = true; 
} 

public void startGame(User user) { 
    user.createPlayer(); 

    this.scene = 1; //Level 1 
} 

public void run() { 
    System.out.println("[Success] Frame Created"); 

    long lastFrame = System.currentTimeMillis(); 
    int frames = 0; 

    loadGame(); 

    while(running) { 
     repaint(); 

     frames++; 

     if(System.currentTimeMillis() - 1000 >= lastFrame) { 
      fps = frames; 
      frames = 0; 
      lastFrame = System.currentTimeMillis(); 
     } 

     try { 
      Thread.sleep(1); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    System.exit(0); 

} 

public class KeyTyped{ 
    public void keyESC() { 
     running = false;; 
    } 
    public void keySPACE() {  
     startGame(user); 
    } 
} 
} 

KeyHandler Класс:

package net.jacobmob.towerdefence; 

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

public class KeyHandler implements KeyListener{ 

private Screen screen; 
private Screen.KeyTyped keyTyped; 

public KeyHandler (Screen screen) { 
    this.screen = screen; 
    this.keyTyped = this.screen.new KeyTyped(); 
} 

public void keyPressed(KeyEvent e) { 
    int keyCode = e.getKeyCode(); 

    System.out.println(keyCode); 

    if(keyCode == 27); 
     this.keyTyped.keyESC(); 

    if(keyCode == 32); 
     this.keyTyped.keySPACE(); 

} 

public void keyReleased(KeyEvent e) { 

} 

public void keyTyped(KeyEvent e) { 

} 
} 

Класс Рама:

package net.jacobmob.towerdefence; 
import javax.swing.JFrame; 

public class Frame extends JFrame{ 

public static void main(String[] args) { 
    new Frame(); 
} 

public Frame() { 
    new JFrame(); 

    this.setTitle("Gigantocypris Tower Defence - by Jacob O.B."); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.setExtendedState(MAXIMIZED_BOTH); 
    this.setUndecorated(true); 
    this.setResizable(false); 
    this.setVisible(true); 

    Screen screen = new Screen(this); 
    this.add(screen); 
} 
} 

ответ

2

ну это определенно не так.

if(keyCode == 27); 
    this.keyTyped.keyESC(); 

if(keyCode == 32); 
    this.keyTyped.keySPACE(); 

вы хотите

if(keyCode == 27){ 
    this.keyTyped.keyESC(); 
} 

if(keyCode == 32){ 
    this.keyTyped.keySPACE(); 
} 
Смежные вопросы