如何在继续方法之前暂停一下 [英] How to make a pause before continuing method

查看:74
本文介绍了如何在继续方法之前暂停一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我知道有人问过这个问题,但是我需要知道如何在html或其他任何东西上都不要这样做.这是我的代码,不包括所有其他Java文件.

Now, I know that this has been asked, but I need to know how to do this NOT on html or anything. Heres my code, not including all of the other java files.

package rtype;

import java.awt.Image;
import java.awt.event.KeyEvent;

import java.util.ArrayList;

import javax.swing.ImageIcon;

public class aa {
private int xd;
private int yd;
private int dx;
private int dy;
private int x;
private int y;
private Image image;

private ArrayList missiles;

private final int CRAFT_SIZE = 70;

public aa() {
    ImageIcon ii = new ImageIcon(this.getClass().getResource("/aa.png"));
    image = ii.getImage();
    missiles = new ArrayList();
    x = 10;
    y = 10;
    xd = -14;
    yd = 140;
}


public void move() {
    if(y >=xd)
        y += dx;
    else if(y < xd)
        y += 1;
    if(y <=yd)
        y += dy;
    else if(y > yd)
        y += -1;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public Image getImage() {
    return image;
}

public ArrayList getMissiles() {
    return missiles;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_SPACE) {
        fire();
    }

    if (key == KeyEvent.VK_UP) {
        dy = -1;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 1;
    }

    if (key == KeyEvent.VK_RIGHT) {
        yd++;
    }

    if (key == KeyEvent.VK_LEFT) {
        yd--;
    }

    if (key == KeyEvent.VK_W) {
        xd++;
    }

    if (key == KeyEvent.VK_S) {
        xd--;
    }
}

public void fire() {
    try{
    missiles.add(new Missle(x + CRAFT_SIZE, y + CRAFT_SIZE));
    }catch(Exception e){}
}

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

    if (key == KeyEvent.VK_UP) {
        dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
    }
}
}

因此,在方法fire()上,我想使其在两次拍摄之间延迟.怎么办?

So, at the method, fire(), I want to make it delay between shots. HOW?

对不起,如果这是n00bish

sorry if this is n00bish

推荐答案

正如已经指出的,Thread.sleep()是您问题的直接答案.

As already has been pointed out, Thread.sleep() is the direct answer to your question.

但是,查看您的代码,我可以看到您想将其用于定时动画,对于Thread.sleep()来说,这不是一个好主意. (我不想在这里过多地尝试,尝试一下,这样做没有什么害处,您很快就会意识到原因.)

However, looking at your code I can see that you want to use it for timing an animation and for that Thread.sleep() isn't a good idea. (I don't want to go into too much detail here, try it, there's no harm in that, and you'll soon realise why.)

例如,您应该拥有一个由三部分组成的系统:

What you should have instead is for example a three-part system:

  1. 第一部分处理输入并将其转换为命令.这些命令将立即放入队列中.
  2. 第二部分是游戏引擎线程.顾名思义,这是一个单独的线程,它执行自己的计时(心跳").您当然可以在这里使用Thread.sleep(),但要记住它有时可能会非常不准确.该引擎从队列中获取命令,对其进行处理并更新内部游戏状态(例如坐标).
  3. 第三部分是显示更新线程.同样,这是与上述两个线程不同的线程,它也将具有自己的单独心跳信号,它们可能以与游戏引擎不同的速度运行,因为您可能要比显示更频繁地更新游戏的内部状态.这是处理实际图像和绘图的部分.
  1. The first part handles input and converts it into commands. The commands are immediately placed in a queue.
  2. The second part is the game engine thread. As the name implies, this is a separate thread, which does its own timing ("heartbeat"). You can of course use Thread.sleep() here, bearing in mind that it can occasionally be wildly inaccurate. This engine takes the commands from the queue, processes them and updates the internal game state (coordinates for example).
  3. The third part is the display update thread. Again, this is a separate thread from the above two, it will also have its own separate heartbeat, which might run at a different speed to the game engine, as you might want to update the internal state of the game more frequently than the display. This is the bit that deals with actual images and drawing.

我知道一开始听起来很繁琐,但是比您原先要做的工作要少得多.

I know it sounds like a lot of work at first and it is but it's a lot less work than what you'd otherwise have to do.

这篇关于如何在继续方法之前暂停一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆