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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:30:43+00:00 2026-06-16T22:30:43+00:00

I am currently working on a space invaders style game but I have run

  • 0

I am currently working on a space invaders style game but I have run into a bit of trouble with multiple instances of bullets. At the moment I can only fire one. I have been trying to get it to work with an Array List but I just can’t seem to get it to work. The closest I got I got it to working was, it fired multiple bullets but they all spawned from the same location as in the bullets didn’t spawn in relation to the ships position. The game also crashed when I removed an object after it exceeded it’s boundary. Can anyone help me to see where I am going wrong. Here is some code that I have so far the parts commented out are my attempts at getting the array list to work

    import java.util.ArrayList;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.GameContainer;

    public class Player extends Entity 
    {
 private int speed = 5;
 private ArrayList<Bullet> bulletList;
 private boolean firing;
 private Bullet bullet;

public Player()
{   
    bullet = new Bullet();
    //bulletList = new ArrayList<Bullet>();
    this.setImage("ship");
    this.setPosition(350,450);
    this.setDimenseions(100, 100);
    this.createRectangle();
}

@Override
public void entityLogic(GameContainer gc, int deltaTime) 
{
    Input input = gc.getInput();

    if(input.isKeyDown(Input.KEY_A))
    {
        this.x -= speed;
    }

    if(input.isKeyDown(Input.KEY_D))
    {
        this.x += speed; 
    }

    if(input.isKeyDown(Input.KEY_W))
    {
        this.y -= speed;
    }

    if(input.isKeyDown(Input.KEY_S))
    {
        this.y += speed;
    }

    if(input.isKeyPressed(Input.KEY_SPACE))
    {
        firing = true;
        bullet.x = this.getX()+40;

        //BulletList.add(new Bullet());
    }

    if(firing)
    {
        /*Carries out the logic for the bullet*/

        //for(Bullet b : bulletList)
        //{
            //b.entityLogic(gc, deltaTime);
        //}

        //Moves the bullet negativly along the y axis
        bullet.entityLogic(gc, deltaTime);
    }
}

@Override
public void entityRendering(Graphics g) 
{
    g.drawImage(this.getImage(), this.getX(), this.getY());

    if(firing)
    {
        /*Draws each bullet object in the list*/

        //for(Bullet b : bulletList)
        //{
            //b.entityRendering(g);
        //}

        bullet.entityRendering(g);
    }
}

}
  • 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-16T22:30:45+00:00Added an answer on June 16, 2026 at 10:30 pm

    First of all forget about your Bullet bullet instance variable. You don’t need it, the list is enough.

    Another thing is that you could use a LinkedList instead that an ArrayList because you don’t need random access and you have to add and remove items frequently, when you iterate over bullets to check for collision use a ListIterator<T> and remove them on the fly.

    Finally it should be something like:

    List<Bullet> bullets = new ArrayList<Bullet>();
    
    public void entityLogic(GameContainer gc, int deltaTime) {
      // since this method is called many times you should shoot a bullet just every X msec
      if (spacebar pressed) {
        // you spawn a new bullet according to player position
        Bullet bullet = new Bullet(player.x,player.y);
        // you add it to the list
        bullets.add(bullet);
      }
    
      // destroy bullets which are outside the viewport
      for (int i = 0; i < bullets.size(); ++i) {
        Bullet bullet = bullets.get(i);
        if (bullet.isOutsideBounds()) {
          bullets.remove(i);
          i--;      
        }
    }
    
    public void entityRendering(Graphics g) {
      for (Bullet bullet : bullets)
        bullets.entityRenering(g);
    }
      }
    

    This is just to give you the basic idea.

    I don’t know slick2d and how it manages the rendering and logic threads, if they are two different threads then you should use a syncronized list, eg:

    List<Bullet> bullets = Collections.synchronizedList(new ArrayList<Bullet>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently working with converting SQLException error messages into messages that are more useful for
Im currently working on a game that uses multi touch to apply zoom to
I'm currently working on dynamically inserting Textblocks into Stackpanels. This has to be done
I am currently working on a project where i need to have an in
I'm working on the project. Currently I'm facing a problem, I have 3 UITableView's
Currently I have a working 3D mesh() plot. However, the vertical axis lends itself
I'm currently working with MS SQL 2005, and have a table that has 17
I'm currently working on a portfolio site wherein I would have to load a
So I have been working with Alfresco for a project I'm currently working on,
I am currently working on a game where I must draw a series of

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.