1

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
  • How do I, per se, pause only execution of the current function? Thread.sleep pauses the entire thread. I only want to pause the current function.
    – Eric Reed
    Dec 4, 2016 at 22:26

4 Answers 4

2

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:

  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.

1

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
}
2
  • Yes, but that would pause the other bullets in the air, the character moving, all of the content in the thread.
    – user1766728
    Nov 2, 2012 at 6:31
  • Perhaps it would have helped to include this in the question. It's probably best to have a fully stated question with a code snippet rather than a short, incomplete question and expecting people to look through your code for the rest
    – HXCaine
    Nov 3, 2012 at 2:15
1

try Thread.sleep(1000) each thousand is a second, for example, if you want it 5sec Thread.sleep(5000) hope i helped you!

0
Thread.sleep(x);

where x is in milliseconds. See the tutorial on sleep() for more info.

1
  • 1
    Do you know if calling Thread.sleep in a keyboard event will block other simultaneous keyboard events?
    – Mike Park
    Oct 25, 2012 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.