Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Remove object from Array without knowing index  (Read 924 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Devvie


Medals: 5



« Posted 2013-06-02 21:20:42 »

Hey guys. I'm working on the collision detection for my pacman game. I've got the pills to load and show on the map, however, in my main class, i'm trying to make it so when the pacman touches one of the pills it removes it from the array. I don't know the index of the pill it hit, so I can't just do pillList.removeIndex(index)..I've tried a bunch of things and been searching to no avail. Any ideas?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void update() {
      //float deltaTime = Gdx.graphics.getDeltaTime();
     
      for (Pills tempPill :  map.getPillList()) {
         tempPill.checkCollision(player.getRect());
         if (tempPill.GetVisible() == false) {
           
         }
      }
     
      keyInput();
     
      player.move(map);
     
   }


The pillList holds all of the pill objects. When the player rectangle overlaps the pill rectangle, it sets the isVisible boolean of the pill object to false..Now i'm trying to iterate through the pill list, and if the isVisible is equal to false, I want to remove the pill from the list.

I can do it like this, but it seems like this is an outdated way to do it..

1  
2  
3  
4  
5  
6  
for (int p = 0; p < map.getPillList().size;p++) {
         map.getPillList().get(p).checkCollision(player.getRect());
         if (map.getPillList().get(p).GetVisible() == false) {
            map.getPillList().removeIndex(p);
         }
      }
Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-06-02 21:31:14 »

You could generate an ID for each pill, then when you hit a pill, you collect its ID, iterate through the list, and match it to get the index.
You could also use a HashMap that links each pill to an ID number, then you could just call map.remove(<ID>).
Offline Bassex96

Senior Devvie


Medals: 5



« Reply #2 - Posted 2013-06-02 21:35:50 »

That's not a bad idea. I haven't used HashMaps yet, so now might be a good time to learn how..I think for now, I may stick with the latter method I posted, as it is working. I'll eventually change it, as this is really just a learning project.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2013-06-02 21:48:48 »

Compare the references and loop the list. Linear search is simple and fast.
Offline davidc

Senior Devvie


Medals: 5
Projects: 2



« Reply #4 - Posted 2013-06-03 01:41:57 »

I took a different approach to my PacMan clone. The map is a grid of values which can represent:
  • a wall
  • corridor without pill
  • corridor with pill

If the player encroaches a certain distance into a corridor cell that has a pill, then the pill is swallowed and cell reverts to "corridor without pill".

This sort of thing won't work as well when you have a whole range of different static things that can occupy a cell at the same time, but PacMan is not such a game. You have pills, big pills and sometimes fruit.
Offline 65K
« Reply #5 - Posted 2013-06-03 04:59:26 »

I think for now, I may stick with the latter method I posted, as it is working. I'll eventually change it, as this is really just a learning project.
The 2nd way is not outdated, it has the advantage of not creating an iterator each time.
But it is not working correctly cause the index is not handled properly and thus checks are skipped (after removal).
Instead of " == false" rather use "if (!temoPill.getVisible())"

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (17 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (248 views)
2017-08-02 12:28:51

cybrmynd (247 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!