当我开始一个线程时Java游戏保持冻结?

Pul*_*nda 1 java if-statement freeze

我正在制作一个上面模式的2D Zombie Shooter,我遇到了一些线程问题.这是交易.每当我按下太空时,我都会让角色射出一颗子弹.问题是,如果你持有空间,它会射击一个,然后暂停,然后射出很多子弹.有很多方法可以解决这个问题,但我希望这样,因为它为未来的拍摄速度变化留出了空间.这是导致问题的线程的代码:

package threads;

import Game.GameCore;

public class Shoot extends GameCore implements Runnable {

/**
 * WHEN I START THIS THREAD, THE ENTIRE GAME FREEZES, AND I DO NOT KNOW
 * WHY... NEED TO FIX. IT DOES NOT FIX THE PROBLEM TO TAKE OUT THE "SHOOT"
 * OR THE "SLEEP"...
 */

public void run() {
    while (shooting && gameRunning) { // shooting is made true when space is
                                        // pressed, and set false when space
                                        // is released. gameRunning is true
                                        // if the game is running, which it
                                        // is. removing either of these
                                        // doesnt work either.
        player.shoot(); // player comes from the GameCore class, and
                        // represents the player entity. if i remove this,
                        // nothing changes.

        try {
            Thread.sleep(bulletShootSpeedMillis); // bulletShootSpeedMillis
                                                    // is equal to 1000, but
                                                    // makes no difference
                                                    // to change
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}
Run Code Online (Sandbox Code Playgroud)

所以这是当然的问题.评论有点指出他们,但生病列出他们.如果我删除明显的东西,例如,player.shoot();Thread.sleep(bulletShootSpeedMillis);甚至是while循环中的一个东西,没有任何变化.问题是,当我初始化线程时,用

else if (key == Integer.parseInt(commands[6])) {
        shooting = true;
        new Thread(new Shoot()).run();
    }
Run Code Online (Sandbox Code Playgroud)

整个游戏都冻结了.一切都没有发生.我用空间开始线程的那一刻,我的游戏冻结了,我无法弄清楚为什么!以前的版本:

else if (key == Integer.parseInt(commands[6])) {
                    player.shoot();
            }
Run Code Online (Sandbox Code Playgroud)

它工作得很好.请帮忙!提前致谢!:)

编辑:谢谢你的快速回答.不用说,XD简单错误的主要学习经验

Hov*_*els 8

Ayyyy!

new Thread(new Shoot()).run(); // ***** !!!!
Run Code Online (Sandbox Code Playgroud)

您不通过调用其run()方法来启动Thread ,因为所有这一切都是在调用代码相同的线程中调用代码.您可以通过调用其方法来启动线程start().