Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX][solved] Removing game objects  (Read 620 times)
0 Members and 1 Guest are viewing this topic.
Offline Apache

Senior Newbie


Projects: 1



« Posted 2014-05-12 01:23:39 »

I was wondering if there were any better ways of removing game objects in my game programmatically by hand. Essentially, once the player collides with the enemy, I want to dispose of the entity entirely.

Is there any better way to do this except for moving the enemy off screen and allowing Javas's garbage collector to flush it's memory usage when I dispose of the current screen/move to a different level?

Offline Nausica
« Reply #1 - Posted 2014-05-12 01:28:49 »

Someone pointed out a while back about using double-buffered ArrayLists to store your entities:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
// static update buffer list A
   protected static List<Entity> entityBufferA = new ArrayList<Entity>(200);
   // static update buffer list B
   protected static List<Entity> entityBufferB = new ArrayList<Entity>(200);
   // byte buffer switch
   protected static byte mainUpdateBuffer = (byte) 0;


public static void refresh(int delta, int screenWidth, int screenHeight) {

      for (Entity e : getMainBuffer()) {
         if(checkIfOnScreen(screenWidth, screenHeight, e)){
            e.render();
         }
         e.update(delta);
      }
     
      Systems.RendererSystem.render();

      for (Entity e : getMainBuffer()) {
         if (e.exists) {
            getOtherBuffer().add(e);
         }
      }
      getMainBuffer().clear();
      mainUpdateBuffer = (byte) (mainUpdateBuffer == 0 ? 1 : 0);
      System.out.println(delta);
   }

   public static List<Entity> getMainBuffer() {
      return (mainUpdateBuffer == 0 ? entityBufferA : entityBufferB);
   }

   public static List<Entity> getOtherBuffer() {
      return (mainUpdateBuffer == 1 ? entityBufferA : entityBufferB);
   }


Just store all your entities into the main buffer when they are created, and when you want to destroy them turn their "exists" boolean to false. This is how I do it,  but I think you get the idea.
Offline Jacob Pickens
« Reply #2 - Posted 2014-05-12 01:32:29 »

Have an ArrayList<Enemy> that is filled with enemy objects. Then have a render method like this:

1  
2  
3  
4  
5  
6  
7  
public void render(Graphics g) {
     for(int i = 0; i < enemies.size(); i++) { //enemies is the arraylist
          TempEnemy = enemies.get(i); // TempEnemy is a temporary enemy set to an enemy in the list so you can do method calls

          TempEnemy.render(g);
     }
}


You would do the same thing with the update loop.

Now you have to be able to add and remove enemies from this list so you would make methods like this:

1  
2  
3  
4  
5  
6  
7  
public void addEnemy(Enemy e) {
     enemies.add(e);
}

public void removeEnemy(Enemy e) {
     enemies.remove(e);
}


Now in your level class or your main game class you have to make this class: (The class that has all these methods about the enemy arraylist along with the arraylist itself)

1  
EnemyController ec = new EnemyController();


Now, to add an enemy you would just have to type:

1  
ec.addEnemy(new Enemy(x, y));


And to remove an enemy:

1  
ec.removeEnemy(enemy instance goes here);


Now you want to be able to render the enemies in the list on screen. So in your render loop add this:

1  
ec.render(g); //G being a graphics object


Now you have drawn all the enemies on the screen. They won't move if you don't call their update method. So in you level's update method put something like this:

1  
ec.update();


Now, you have it. In your collision event for your player you might want to put something like this:

1  
2  
3  
4  
if(collision with enemy) {
     ec.removeEnemy(enemy);
     score++;
}


You would want to pass the EnemyController to the player in a constructor argument BTW.

Fell free to drop a medal! Have fun!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Apache

Senior Newbie


Projects: 1



« Reply #3 - Posted 2014-05-12 01:39:53 »

Awesome! Thank you guys.
Offline Longarmx
« Reply #4 - Posted 2014-05-12 01:51:01 »

Just removing the entity from an arraylist should suffice. As long as there isn't an instance left of that object them the garbage collector will remove it from memory.

Another option would be to have a Boolean in the entity isDead. When it collides with the player, you would set this to true. When you're updating and rendering then you would just ignore the enemies that are dead. When you switch the level or dispose of the screen then they should all be deleted.

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2014-05-12 01:57:50 »

The two-ArrayList way is the best, although it is best and fastest to simply swap references instead of using a method call with a byte flag for every single add.

Offline GoToLoop
« Reply #6 - Posted 2014-05-12 02:18:49 »

Another option would be to have a Boolean in the entity isDead. When it collides with the player, you would set this to true. When you're updating and rendering then you would just ignore the enemies that are dead.

That's my favorite strategy! Although I prefer to save the "dead" object's index value
and then re-use it when requesting a new enemy rather than instantiate another 1!  Cool  

The other option is from this recent thread:
http://www.java-gaming.org/topics/concurrentmodificationexception-when-removing-an-item-from-an-arraylist/32939/msg/309008/view.html

If the order doesn't matter, remove the latest (tail) entry and insert it in the index of the "dead" object, replacing it!   Tongue
This way, we avoid left-shifting the List's entries completely! Shocked

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
static final int BULLETS = 100;
final List<Bullet> bullets = new ArrayList<>(BULLETS);

public void displaySpritesOptimized() {
  stroke(Bullet.COLOUR);
  for (int b = bullets.size(), len = b; b-- != 0;)
    if (bullets.get(b).script()) { // returns true if requesting remove due to outta canvas!
      final Bullet u = bullets.remove(--len);
      if (b != len)  bullets.set(b, u);
      System.out.println("A bullet has strayed too far from view!");
    }
}
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!