0

我一直在关注 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();
    }
    }

谢谢!

4

1 回答 1

4

这可能是罪魁祸首:

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

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

Main jg = new Main();
//Threads
Thread t1 = new Thread(jg);
t1.start();
于 2013-08-12T22:03:44.693 回答