0

我正在制作一个游戏,用户可以在其中找到一份“工作”,总收入会随着他们的薪水而增加,它会暂停一秒钟,然后重复。它就像 Cookie Clicker 中的 cookie 系统,您可以在其中制作一些 cookie,它会暂停,然后显示更多。在游戏中,你点击一个按钮,你就会得到一份“工作”。

//The button to get a job
                    JButton workButton = new JButton("Get a job");
                    mainLayout.gridx = -1;
                    mainLayout.gridy = 1;
                    mainPanel.add(workButton, mainLayout); 
                    workButton.addActionListener(new ActionListener(){

                        @Override
                        public void actionPerformed(ActionEvent arg0) {

                            boolean jobButtonClicked = true;
                            Random jobGenerator = new Random();
                            int jobSalary = jobGenerator.nextInt(200);
                            workLabel.setText("You are making $" +jobSalary);
                            int totalMoney = 0;

                            for(jobButtonClicked = true;;){

                                totalMoney = totalMoney + jobSalary;
                                //I want the total Money to increase by jobSalary, 
                                //pause for one second, and then do it again.

                            }


                        }

                });

我希望在这里发生暂停:

for(jobButtonClicked = true;;){

                            totalMoney = totalMoney + jobSalary;
                            //I want the total Money to increase by jobSalary, 
                            //pause for one second, and then do it again.

我尝试了“Thread.sleep();”,但如果我不填写参数,则会收到错误“Thread 类型中的方法 sleep(long) 不适用于参数 ()”。如果我将毫秒数放在括号中,我会得到错误:“未处理的异常类型 InterruptedException” 我是这个东西的初学者,我这样做是为了学习。如果可以的话请帮忙。谢谢你的时间。

4

1 回答 1

1

try{
    Thread.sleep(1000);
} catch (InterruptedException e){}

不要忘记为您的循环创建一个新线程。将事件调度线程用于休眠的东西是不好的,因为它会使您的应用程序冻结。

于 2013-10-03T23:00:57.980 回答