0

几天后我有一个项目到期,我在 Eclipse 中的代码没有显示任何错误,也没有任何警告,但游戏(JFrame)不会出现。我相信这个错误与我做运动的方式有关

this.addKeyListener(movement);

欢迎任何想法!

主.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Main extends JFrame implements Runnable {
private Movement movement = new Movement();

public int width = 800;
public int height = 600;
public int fps = 1000;
public int score;
public int charX;
public int charY;
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;

public Color cytoplasm = new Color(50,130,255);

public Image character;

public Thread game;

//Double Buffer
private Image dbImage;
private Graphics dbg;

public Main() {
    //Images
    ImageIcon characterImage = new ImageIcon("F:/workplace/com.thecellsells.lysosome/src/com/thecellsells/lysosome/Lysosome.gif");
    character = (characterImage.getImage());

    //Game Properties
    setSize(width, height);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(cytoplasm);
    this.addKeyListener(movement);


    //Threads
    game = new Thread(this);
    game.start();

    //Other
    charY = height/2 - 10;
    charX = width/2 - 16;
}

public void paint(Graphics g) {
    //Double Buffer
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);


}

public void paintComponent(Graphics g) {
    g.drawImage(character, charX, charY, this);
    repaint();
}

public static void main(String[] args) {
    @SuppressWarnings("unused")
    Main Main = new Main();
}

@Override
public void run() {
    while (true) {
        fpsSetter();
    }
}

//FPS -- set's how fast game runs
@SuppressWarnings("static-access")
public void fpsSetter() {
    try {
        game.sleep(fps/fps);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

运动.java

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

@SuppressWarnings("serial")
public class Movement extends Main implements KeyListener {
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;
public void keyPressed (KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W ) {

        bCharUp = true;
        if (bCharUp) {
            charY -= 1;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
        bCharLeft = true;
        if (bCharLeft) {
            charX -=1;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_W ) {
        bCharDown = true;
        if (bCharDown) {
            charY -=1;
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
        bCharRight = true;
        if (bCharRight) {
            charX +=1;
        }
    }

}
@Override
public void keyReleased (KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W ) {
        bCharUp = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
        bCharLeft = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_S) {
        bCharDown = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
        bCharRight = false;
    }
}
@Override
public void keyTyped(KeyEvent e) { }        
}

托马斯

4

3 回答 3

1

在您的代码中,不要扩展 Movement 中的 Main 类,您正在重载堆栈!

注意:在您的按键中,向下应该是 KeyEvent.VK_S ,这样它就不会上升。

试试这个。

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

public class Movement implements KeyListener {
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;
public int charX;
public int charY;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;

public Movement(int charX , int charY){
this.charX = charX;
this.charY = charY;

}

public Movement(){
this(0,0);
}

public void keyPressed (KeyEvent e) {
System.out.println("" + e.toString());
if (e.getKeyCode() == KeyEvent.VK_W ) {

    bCharUp = true;
    if (bCharUp) {
        charY -= 1;
    }
}
if (e.getKeyCode() == KeyEvent.VK_A) {
    bCharLeft = true;
    if (bCharLeft) {
        charX -=1;
    }
}
if (e.getKeyCode() == KeyEvent.VK_S ) {
    bCharDown = true;
    if (bCharDown) {
        charY -=1;
    }
}
if (e.getKeyCode() == KeyEvent.VK_D) {
    bCharRight = true;
    if (bCharRight) {
        charX +=1;
    }
}

}
@Override
public void keyReleased (KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W ) {
    bCharUp = false;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
    bCharLeft = false;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
    bCharDown = false;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
    bCharRight = false;
}
}
@Override
public void keyTyped(KeyEvent e) { }        

运动类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Main extends JFrame implements Runnable {
private Movement movement;

public int width = 800;
public int height = 600;
public int fps = 1000;
public int score;
public int charUp;
public int charDown;
public int charLeft;
public int charRight;
public int movementSpeed = 5;
public int movementFrame = 0;
public int movementDiagonal = 10;

public boolean bCharUp = false;
public boolean bCharDown = false;
public boolean bCharLeft = false;
public boolean bCharRight = false;

public Color cytoplasm = new Color(50,130,255);

public Image character;

public Thread game;

//Double Buffer
private Image dbImage;
private Graphics dbg;

public Main() {
movement = new Movement();
//Images
ImageIcon characterImage = new ImageIcon("image here");
character = (characterImage.getImage());

//Game Properties
setSize(width, height);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(cytoplasm);
this.addKeyListener(movement);


//Threads
game = new Thread(this);
game.start();

//Other
movement.charY = height/2 - 10;
movement.charX = width/2 - 16;
}

public void paint(Graphics g) {
//Double Buffer

dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);


}

public void paintComponent(Graphics g) {
g.drawImage(character, movement.charX, movement.charY, this);
repaint();
}

public static void main(String[] args) {
@SuppressWarnings("unused")
Main Main = new Main();
}

@Override
public void run() {
while (true) {
    fpsSetter();
}
}

//FPS -- set's how fast game runs
@SuppressWarnings("static-access")
public void fpsSetter() {
try {
    game.sleep(fps/fps);
} catch (Exception e) {
    e.printStackTrace();
}
}
}
于 2013-10-08T17:16:20.403 回答
0

尝试使用setPreferredSize(width, height);.

编辑:问题在于您扩展了运动类中的主类。因此,当您启动程序时,运动类调用 Main 类并再次初始化运动类,从而形成竞争条件。只是不要扩展 Main 类并使变量charXcharY静态。因此,您可以使用 Main.. 在任何地方访问变量。

于 2013-10-08T16:48:31.900 回答
0

在您的 main() 方法中,您正在创建 Main 对象(从 JFrame 扩展而来),但您尚未将其设置为可见。

于 2013-10-08T17:34:33.270 回答