2015-04-04 63 views
-2
package com.Multiply.com; 

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Core extends JPanel { 
public static void main(String[] args){ 
    Core game = new Core(); 

    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    game.frame.setTitle("Multiply"); 
    game.frame.setIconImage(testIcon); 
    game.frame.add(game); 
    game.frame.pack(); 
    game.frame.setLocationRelativeTo(null); 
    game.frame.setResizable(false); 
    game.frame.setVisible(true); 
    t1.start(); 

    running = true; 
    Collision.charCollision(); 

} 

//start of variables 
private static final long serialVersionUID = 1L; 
public static int width = 1280; //width and height of the screen 
public static int height = 720; 

public static boolean running = false; 

public static int charX = width /2 - 50; 
public static int charY = height /2 - 50; 
public static int charSizeX = 50; 
public static int charSizeY = 50; 
public static double charSpeed = 10; 

public static Image testIcon = Toolkit.getDefaultToolkit().getImage("Multiply_Icon.png"); //Icon variables, locates the icon in the folder 

JFrame frame = new JFrame(); //the frame variable 

static Thread t1 = new Thread(); 

public Core(){ 
    KeyListener listener = new MyKeyListener(); 
    addKeyListener(listener); 
    setFocusable(true); 

    Dimension size = new Dimension(width, height); //variable that sets the size of the window to 1280x720p 
    setPreferredSize(size);       //Sets the size of the window to "size" 

    frame = new JFrame();       //makes frame into the JFrame window 
} 

public class MyKeyListener implements KeyListener { 
    @Override 
    public void keyPressed(KeyEvent e) { 
     int keyCode = e.getKeyCode(); 
     //System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode())); 
     if(keyCode == KeyEvent.VK_W){ 
      charY-= charSpeed; 
      repaint(); 
     } 
     else if(keyCode == KeyEvent.VK_A){ 
      charX-= charSpeed;; 
      repaint(); 
     } 
     else if(keyCode == KeyEvent.VK_S){ 
      charY+= charSpeed; 
      repaint(); 
     } 
     else if(keyCode == KeyEvent.VK_D){ 
      charX+= charSpeed; 
      repaint(); 
     } 
    } 
    @Override 
    public void keyReleased(KeyEvent e) { 
     int keyCode = e.getKeyCode(); 
     //System.out.println("keyReleased="+KeyEvent.getKeyText(e.getKeyCode())); 
     if(keyCode == KeyEvent.VK_ESCAPE){ 
      ConfigFileWriter cfw = new ConfigFileWriter(); 
      if(cfw.wroteFile == false){ 
       cfw.openFile(); 
       cfw.writeFile(); 
       cfw.closeFile(); 
       cfw.wroteFile = true; 
       System.out.println("wroteFile = true"); 
       } 
      running = false; 
      System.out.println("Escape"); 
      try { 
       t1.join(); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 
      System.exit(0); 
     } 
     else if(keyCode == KeyEvent.VK_OPEN_BRACKET){ //To be removed, testing purposes 
      charSpeed-= 1; 
     } 
     else if(keyCode == KeyEvent.VK_CLOSE_BRACKET){ 
      charSpeed+= 1; 
     } 
     else if(keyCode == KeyEvent.VK_L){ 
      ConfigFileReader cfr = new ConfigFileReader(); 

      cfr.openFile(); 
      cfr.readFile(); 
      cfr.closeFile(); 
      charSpeed = Double.parseDouble(cfr.speed); 
     } 
    } 
    @Override 
    public void keyTyped(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 

public void paint(Graphics g) {  // Draws top first to bottom last 
    Graphics2D g2 = (Graphics2D) g; 

    Image background = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Black_Background.png"); 
    Image character = Toolkit.getDefaultToolkit().getImage("C:/Users/Kanin/Desktop/Multiply_Game_Engine/Images/Character.png"); // C:/Users/Kanin/Desktop/Multiply_Game_Engine/ 
    g2.drawImage(background, 0, 0, this); //draws the black background 
    g2.drawImage(character, charX, charY, charSizeX, charSizeY, this); 
} 

}的Java不需要的代码行,但我不知道为什么

package com.Multiply.com; 

public class Collision extends Core { 
private static final long serialVersionUID = 1L; 
public static void charCollision(){ 
    while(running){ 
     System.out.println("Running"); //Won't detect unless this is here for some reason. 
     if(charX < -1){   //Character can't go past the left side of the screen. 
      charX = 0; 
     } 
     else if(charX > 1240){ //Character can't go past the right side of the screen. 
      charX = 1240; 
     } 
     else if(charY < -1){ //Character can't go above the top side of the screen. 
      charY = 0; 
     } 
     else if(charY > 680){ //Character can't go below the bottom side of the screen. 
      charY = 680; 
     } 
    } 
} 
} 

近极顶它说 的System.out.println( “暗战”); 如果我没有该行不注册,我的块走出去的边界,它只是不断去。当我运行游戏时,如果我删除了system.out代码块,它甚至不会在if语句中运行任何内容

+0

嗯......不会检测_what_? _什么?继续前进?你在哪里声明你正在使用的变量?似乎有很多信息缺失。 – ajb 2015-04-04 06:05:34

+2

你能分享整个代码吗?这段代码并没有描述while(running)中“running”的值是什么, – 2015-04-04 06:06:26

回答

1

我的猜测是您注册的KeyListener是在与main螺纹(由GUI框架催生了线程之一),但是Collision.charCollision方法是在主线程运行。 当离开了System.out.println声明,在方法charCollision变量被认为是自包含的虚拟机,但只要你插入与控制台交互(并因此与其他线程交互)可变running保持在与GUI线程同步。 您可以尝试申报在GUI中,以及在你的主线程中使用的所有变量volatile,看看是否已解决您的问题。 关于多线程的更多信息,请参见Loop doesn't see changed value without a print statement

相关问题