Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:13:49+00:00 2026-06-13T11:13:49+00:00

Now, I know that this has been asked, but I need to know how

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:13:50+00:00Added an answer on June 13, 2026 at 11:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I know this has been asked before. But I've made doubly sure that
I know this question has already been asked but I am in urgent need
I know this type of question has been asked before, but I could not
I know that this has been asked many times before, but I believe my
I know this has been asked many times but its been more for MVC1.
I know this question has been asked a couple of times, but there's something
I know this has been asked before but none of the cases I've seen
I know this has been asked before (and I will keep researching), but I
i know this has been asked but none of the threads had a solution
hello i know this has been asked and answered many times but i cant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.