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
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:
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).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.