Java游戏中的角色没有响应

时间:2013-08-12 21:57:11

标签: java

我一直在关注Java游戏教程并且坚持一个,那个就在这里:http://www.youtube.com/watch?v=RolMRqekV3o&list=PL6E90696571998DC2我有一个问题,我的角色没有响应箭头键。如果你能提供帮助就会很棒。我的代码:

package theJavaHubGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Main extends JFrame implements Runnable{

    int x, y, xDirection = 10, yDirection = 10;
    private Image dbImage;
    private Graphics dbg;
    Image face;

    public void run(){
        try{
            while(true){

                move();

                Thread.sleep(5);
            }
        }
        catch(Exception e){
            System.out.println("Error");

        }
    }

    Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 15);

        public void move(){
            x += xDirection;
            y += yDirection;

            if(x <= 0)
                x = 0;
            if (x >= 200)
                x = 200;
            if(y <= 50)
                y = 50;
            if(y >= 250)
                y = 250;
        }

        public void setXDirection(int xdir){
            xDirection = xdir;
        }

        public void setYDirection(int ydir){
            yDirection = ydir;
        }
        public class AL extends KeyAdapter{
            public void keyPressed(KeyEvent e){
                int keyCode = e.getKeyCode();

                if(keyCode == e.VK_LEFT){
                    setXDirection(-1);
                    System.out.println("Key Pressed");
                }

                if(keyCode == e.VK_RIGHT){ 
                    setXDirection(+1);

                }

                if(keyCode == e.VK_UP){
                    setYDirection(-1);
                }

                if(keyCode == e.VK_DOWN){
                    setYDirection(+1);
                }

            }

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

                if(keyCode == e.VK_LEFT){
                    setXDirection(0);
                    System.out.println("Key Pressed");
                }
                if(keyCode == e.VK_RIGHT){
                    setXDirection(0);
                }
                if(keyCode == e.VK_UP){
                    setYDirection(0);
                }
                if(keyCode == e.VK_DOWN){
                    setYDirection(0);
                }
            }
        }

    public Main(){

        //Load Images
        ImageIcon i = new ImageIcon("F:/Android/Workspace/TheJavaHubGame/src/theJavaHubGame/Face.gif");
        face = i.getImage();

        //Game Properties
        addKeyListener(new AL());
        setTitle("Java Game");
        setSize(250,250);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBackground(Color.GREEN);

        x=150;
        y=150;
    }

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

    public void paintComponent(Graphics g){
        g.setFont(font);
        g.setColor(Color.MAGENTA);
        g.drawString("Super-Mega Ball Bouncer II", 50, 50);
        g.setColor(Color.red);
        g.fillOval(x, y, 15, 15);
        g.drawImage(face, x, y, this);
        repaint();
    }


    public static void main(String[] args){
        Main jg = new Main();
        //Threads
        Thread t1 = new Thread();
        t1.start();
    }
    }

谢谢!

1 个答案:

答案 0 :(得分:4)

这可能是罪魁祸首:

Main jg = new Main();
//Threads
Thread t1 = new Thread();
t1.start();

您没有将Runnable传递给Thread,因此线程实际上并没有做任何事情。没有更新线程,游戏中的任何内容都不会移动。试试这个:

Main jg = new Main();
//Threads
Thread t1 = new Thread(jg);
t1.start();