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 7050097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:06:07+00:00 2026-05-28T03:06:07+00:00

I am coding a space invaders clone, I need the invaders to do their

  • 0

I am coding a space invaders clone, I need the invaders to do their usual movement but pause for one second before moving again.

EG. Move 5 pixels, then wait, then move another 5 pixels.

The method called moveInvaders() is the method I want to make wait for 1000ms. Below is that such method.

private void moveInvaders() {
        if(direction == "right") {
        if(invaderGreenEight.getX() == 455 || invaderRedEight.getX() == 455 || invaderBlueEight.getX() == 455) {
            direction = "left"; // Change direction to left
        }
    } else {
        if(invaderGreenOne.getX() == 15 || invaderRedOne.getX() == 15 || invaderBlueOne.getX() == 15) {
            direction = "right"; // Change direction to right
        }
    }

    if(direction == "right") {
        // Move Green Invaders Right
        invaderGreenOne.moveX(1);
        invaderGreenTwo.moveX(1);
        invaderGreenThree.moveX(1);
        invaderGreenFour.moveX(1);
        invaderGreenFive.moveX(1);
        invaderGreenSix.moveX(1);
        invaderGreenSeven.moveX(1);
        invaderGreenEight.moveX(1);
        // Move Red Invaders Right
        invaderRedOne.moveX(1);
        invaderRedTwo.moveX(1);
        invaderRedThree.moveX(1);
        invaderRedFour.moveX(1);
        invaderRedFive.moveX(1);
        invaderRedSix.moveX(1);
        invaderRedSeven.moveX(1);
        invaderRedEight.moveX(1);
        // Move Blue Invaders Right
        invaderBlueOne.moveX(1);
        invaderBlueTwo.moveX(1);
        invaderBlueThree.moveX(1);
        invaderBlueFour.moveX(1);
        invaderBlueFive.moveX(1);
        invaderBlueSix.moveX(1);
        invaderBlueSeven.moveX(1);
        invaderBlueEight.moveX(1);
    }
    if(direction == "left") {
        // Move Green Invaders Left
        invaderGreenOne.moveX(-1);
        invaderGreenTwo.moveX(-1);
        invaderGreenThree.moveX(-1);
        invaderGreenFour.moveX(-1);
        invaderGreenFive.moveX(-1);
        invaderGreenSix.moveX(-1);
        invaderGreenSeven.moveX(-1);
        invaderGreenEight.moveX(-1);
        // Move Red Invaders Right
        invaderRedOne.moveX(-1);
        invaderRedTwo.moveX(-1);
        invaderRedThree.moveX(-1);
        invaderRedFour.moveX(-1);
        invaderRedFive.moveX(-1);
        invaderRedSix.moveX(-1);
        invaderRedSeven.moveX(-1);
        invaderRedEight.moveX(-1);
        // Move Blue Invaders Right
        invaderBlueOne.moveX(-1);
        invaderBlueTwo.moveX(-1);
        invaderBlueThree.moveX(-1);
        invaderBlueFour.moveX(-1);
        invaderBlueFive.moveX(-1);
        invaderBlueSix.moveX(-1);
        invaderBlueSeven.moveX(-1);
        invaderBlueEight.moveX(-1);
    }
}

The game is already a thread and the method in question is in the same class so issuing Thread.sleep(1000); just pauses the entire game for 1 second, but I just want the execution of one method to pause.

If you want more information I am more than happy to provide it.

GeissT

  • 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-05-28T03:06:07+00:00Added an answer on May 28, 2026 at 3:06 am

    The moveInvaders() method should keep track of a member variable which stores the time the invaders were last moved (e.g. as a long number of milliseconds), this way, any time it is called it can check the value of that variable – if enough time has elapsed then the invaders can move, if not, then the method will simply return without doing anything. For example:

    protected long invadersLastMovedMillis;
    
    protected void moveInvaders() {
      long currentTime = System.currentTimeMillis();
      if ((invadersLastMovedMillis != 0)
          && ((currentTime - invadersLastMovedMillis) < 1000L)) {
        return; // No need to move them yet.
      }
      // March on, space invaders!
      invadersLastMovedMillis = currentTime;
    }
    

    You don’t want to actually cause the thread to sleep because the game will then become unresponsive!

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

Sidebar

Related Questions

Coding footer naively, if there's not enough content, then there will be empty space
I need a regular expression to validate string with one or more of these
I'm in the design/skeleton coding phase of my C++ game with Lua scripting, but
W3C recommends putting a space before the closing tag in XHTML , because this
What is the formula to figure out how much space an index will need
I need to change my coding style of putting opening braces in same line
I realize that this rule might differ from one company's coding standards to another,
I have found useful regex expressions from the site, but this particular one eludes
While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend
Coding i came around to check for the vararg performance of Java. I write

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.