Reputation: 89
I have a method where I'm trying to use my own iterator to access two different objects in my Collection of game objects. I am first trying to access a foodstation(object) and get it's capacity and store it into fStationCapacity. Then I try and access my Ladybug (Object) and increase it's foodlevel by the fStationCapacity, but I just can't get it because it never reaches the if the statement. How can I try and fix this?
public void init() {
theGameCollection = new GameObjectCollection();
theGameCollection.add(Ladybug.getLadyBug());
theGameCollection.add(new Flag(210.0,100.0));
theGameCollection.add(new Flag(800.5, 200.2));
theGameCollection.add(new Flag(364.5, 754.0));
theGameCollection.add(new Flag(568.5, 422.5));
theGameCollection.add(new Flag(540.0,280.0));
theGameCollection.add(new Flag(954.0, 155.2));
theGameCollection.add(new Flag(666.0, 689.0));
theGameCollection.add(new Flag(489, 900.5));
theGameCollection.add(new Flag(600.4, 777.5));
theGameCollection.add(new Spider());
theGameCollection.add(new Spider());
theGameCollection.add(new FoodStation());
theGameCollection.add(new FoodStation());
}
public void collisionWithFood() {
IIterator theObjects = theGameCollection.getIterator();
IIterator theObjects2 = theGameCollection.getIterator();
int fStationCapacity = 0;
int foodlevel= 0;
while (theObjects.hasNext()) {
GameObject go = (GameObject) theObjects.getNext();
while (theObjects2.hasNext()) {
GameObject go2 = (GameObject) theObjects2.getNext();
if ((go instanceof FoodStation && go2 instanceof Ladybug) && ((FoodStation)go).getCapacity() != 0 ) {
fStationCapacity = ((FoodStation)go).getCapacity(); //gets the food station capacity and stores into into fStationCapacity
((FoodStation)go).reduceCapacity(); //reduces the food station's capacity food level to zero
((FoodStation)go).setColor(144, 238, 144); //changes the color of the food station to light green
((Ladybug)go2).increaseFoodLevel(fStationCapacity);
System.out.println("LadyBug has collided with a foodStation of capacity: " + fStationCapacity);
System.out.println("Capacity" + fStationCapacity + "\nCurrent Food Level" + foodlevel);
break; //breaks because I only want one instance
}
}
}
theGameCollection.add(new FoodStation()); //creates a new food station with random location and random size
}
Upvotes: 0
Views: 38
Reputation: 984
probably theGameCollection.getIterator() always returns the same iterator. instead you should create 2 different iterators for the two loops
Upvotes: 1