问题
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;
}
}
}
So, at the method, fire(), I want to make it delay between shots. HOW?
sorry if this is n00bish
回答1:
As already has been pointed out, Thread.sleep()
is the direct answer to your question.
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:
- The first part handles input and converts it into commands. The commands are immediately placed in a queue.
- 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). - 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.
回答2:
You can use Thread.sleep(1000);
to sleep for 1000 milliseconds (1 second).
Note that you'll have to catch an InterruptedException:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Handle here
}
回答3:
try Thread.sleep(1000)
each thousand is a second, for example,
if you want it 5sec Thread.sleep(5000)
hope i helped you!
回答4:
Thread.sleep(x);
where x
is in milliseconds. See the tutorial on sleep() for more info.
来源:https://stackoverflow.com/questions/13078548/how-to-make-a-pause-before-continuing-method