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

  • SEARCH
  • Home
  • 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 8679609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:00:59+00:00 2026-06-12T21:00:59+00:00

I have a shooting game and when the spacebar is pressed, a bullet object

  • 0

I have a shooting game and when the spacebar is pressed, a bullet object is created. When the bullet object is outside of the screen, I want to delete the object so I can shoot more than 10 times.

Instantiation:

Shot[] shot = new Shot[10];

Spacebar code:

if (key == KeyEvent.VK_SPACE) {
    if (shots < maxShots) {
        if (canShoot) {
            shot[shots] = new Shot(player.x, player.y, player.width, player.height, shotDir);
            shots++;
            canShoot = false;
        }
    }
}

Drawing code:

for (int i = 0; i < shots; i++) {
    if (shot[i].active) {
        shot[i].drawShot(g);
    }
}

Where I want to delete the shot:

for (int i = 0; i < shots; i++) {
    shot[i].shotLoop();
    if (shot[i].outOfBounds(WINDOW_SIZE.width, WINDOW_SIZE.height)) {
        // Delete Bullet
        shot[i].active = false;
    }
}

I tried to do shots--; but when one shot leaves the screen, all shots currently on the screen are deleted. Any suggestions?

Here is the shot[i].outOfBounds() code

public boolean outOfBounds(int screenx, int screeny) {
    if (x < 0 || x > screenx || y < 0 || y > screeny) {
        return true;
    } else {
        return false;
    }
}
  • 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-12T21:01:00+00:00Added an answer on June 12, 2026 at 9:01 pm

    Use an ArrayList instead so you can remove it from the list when it goes out of bounds?

    ArrayList<Shot> shot = new ArrayList<Shot>();
    

    ArrayList.remove(int index)

    You shouldn’t remove while iterating over, so you might have to store the index of the objects (or the objects themselves) and remove them after finding the bullets to remove.

    Also if you only want 10, you can set a capacity of 10, and just monitor the size to make sure it doesn’t go over.

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

Sidebar

Related Questions

I have created a simple flash game similar to shooting balloons. You can find
I have writing a small shooting game in cocos2d. Winning the game is to
I have a simple shooting game where I have a cannon in the center
In my number game children have 30 seconds to shoot the number icons that
i am currently creating a shooting game , currently i have a main class
I'm creating a shooting game where you touch the screen and the player shoots.
I have created a game which paints images to a JPanel. This runs with
I have trouble shooting at file download problem. In Chrome, the download works fine.
have written this little class, which generates a UUID every time an object of
Im creating a space shooter game for android, im currently implementing the shooting methods

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.