0

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
}

1 Answer 1

1

probably theGameCollection.getIterator() always returns the same iterator. instead you should create 2 different iterators for the two loops

Sign up to request clarification or add additional context in comments.

1 Comment

I was confusing myself just needed to have two while loops seperate of each with their own gameobject. I was confusing myself with the foodlevel variable I don't know why I even had it in there it was not needed at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.