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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:36:13+00:00 2026-06-16T09:36:13+00:00

I was creating an sscce, but there was just way too much code to

  • 0

I was creating an sscce, but there was just way too much code to post it here, so I will post sections and hopefully you can follow!

So, I have this class called Ship, within the class there is this:

Chunk 1:

keyboard.keyPress("SPACE", new Runnable(){

    @Override
    public void run(){
        Laser laser = new Laser(
                new Sprite("/media/images/laser.png"),
                sscce.Ship.this.room);
        sscce.Ship.this.room.addGameObjectAt(
                laser,
                sscce.Ship.this.getX() + (sscce.Ship.this.getWidth() / 2) - 3,
                sscce.Ship.this.getY());
    }
});

When the Space key is press it creates a Laser object and then we add it to the game where this object is stored within an ArrayList then draw to the screen in my main game loop, which loops through the ArrayList.

Laser is a very simple class, it looks like this:

Chunk 2:

package JGame.GameObject;

import JGame.Room.Room;

public class Laser extends GameObject{

    public Laser(Sprite sprite, Room room){
        super(sprite, room);
        move.moveToY(0 - this.getHeight(), 5, new Runnable(){
            @Override
            public void run(){
                Laser.this.destroy.destroyGameObject();
            }
        });
    }
}

So Chunk 1 create Chunk 2 then adds it to the array list. This all works fine and dandy!

Chunk 3:

public void moveToY(int y, int speed, Runnable complete){
    this.obj.moveEndY = y;
    this.obj.moveAmountY = speed;
    this.complete = complete;
    this.obj.needsToMoveY = true;
}

This adds a move action to the newly created Laser. And then we move the object vertically (defined by chunk 2 and performed within the main game loop).

This next part is breaking the game, and by break I mean every thing stops (eventlisteners, movement, etc).

From Chunk 2 I say “When moveToY gets to its destination run the Runnable code” and the runnable code says to destroy the laser using destroyGameObject(). and the destory looks like this and is initialized in all of the Game Objects Constructors (Laser, Ship etc).

Chunk 4:

package JGame.Actions;

import JGame.GameObject.GameObject;
import JGame.Room.Room;

public class DestroyAction extends Action{
    GameObject obj;
    Room room;
    public DestroyAction(Room room, GameObject obj){
        this.room = room;
        this.obj = obj;
    }

    public void destroyGameObject(){
        room.removeGameObject(this.obj);
    }
}

Chunk 4 just tells the room “Hey we need to remove this object from the list”

Chunk 5:

public class Room extends JPanel implements Runnable{

    ArrayList<GameObject> gameObjects = new ArrayList<>();

    public void run(){
        try{
            while(true){
                // Do Event Listeners
                // Move objects along Y axis
                for(GameObject go : gameObjects){
                    if(go.needsToMoveY){
                        int objY = go.getY();
                        if(objY > go.moveEndY){
                            go.setY(objY - go.moveAmountY);
                        }else if(objY < go.moveEndY){
                            go.setY(objY + go.moveAmountY);
                        }else{
                            go.needsToMoveY = false;
                            // Executes Runnable parameter from moveToY()
                            go.move.actionComplete();
                        }
                    }
                }
                this.repaint();
                Thread.sleep(roomSpeed);
            }
        }catch(Exception e){
        }
    }

    public void removeGameObject(GameObject go){
        gameObjects.remove(go);
        System.gc();
    }
}

So, finally here in Chunk 5, this is the main loop a lot of code has been striped out but this tests if it should be moving, if it is done, run actionComplete() which was passed in via moveToY() as the Runnable. Well, this Runnable says delete the object when it gets to it’s location. And when it runs the code within the Runnable 1 time, EVERYTHING stops. Movements stop; so if 2+ lasers were fired they should move upwards but they stop at their position and don’t move. Keyboard buttons stop working, so the arrows to move the Ship no longer respond so you can navigate the ship up/down/left/right it just sits at it’s last position of at the point where the Runnable was triggered for the first time.

So… Why is everything stopping? I just want to remove the object that was in the Runnable.

  • 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-16T09:36:15+00:00Added an answer on June 16, 2026 at 9:36 am

    You have a serious problem with your code organization: you are modifying the gameObjects list in Room directly (in method removeGameObject) while at the same time going through the list with an iterator (in method run). This will cause the iterator to complain (as in throw an exception). Note that you are catching—and silently ignoring!—exceptions in your main run method. Never throw away exceptions like that—they will tell you when something goes wrong.

    You need to organize your code so that you remove objects from the list only when there is no iterator traversing the list. You can do this by creating a way to mark objects for removal and then remove them in a separate step of your game loop.

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

Sidebar

Related Questions

Creating a batch script in Windows XP. Here's a snippet of code I'm having
Creating the closure is easy but using it is confusing for me. Here is
Creating a server-side socket will fail if I'm trying to use the same port
Creating shell output for a game, but don't want the window to scroll with
Creating a new post in wordpress, I try to set a featured image. The
Creating an Iphone application, I used to perform INSERT QUERY on different databases. But
Creating a simple TCP server based on examples but still do not get how
Creating an ATL project in MSVC seems to create not one but two projects;
Creating a link in PHP: echo <a href=\$currentFile?rowID=$row['id']\>click here</a>; This throws an error: Parse
Creating a searchable contacts kind of app. Below is my code public class EmployeeList

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.