This site requires JavaScript, please enable it in your browser!
Greenfoot back
I.AM.A.LION<ROAR>
I.AM.A.LION<ROAR> wrote ...

2013/5/1

HELP ME PLEASE

I.AM.A.LION<ROAR> I.AM.A.LION<ROAR>

2013/5/1

#
basically,i am doing a game where you start with a shopping trolley and you have to go round collecting food items. but your only aloud to collect the items on your list & you have to dodge the items that arnt on the list. what i want to know is how do you make a shopping list where you can cross out the item once you have collected it?
Gevater_Tod4711 Gevater_Tod4711

2013/5/1

#
If you want a list of items you have to use an array or java.util.List. The array is easyer to use so you better take this. So at the beginning you need an array (eigher a String array or an Actor array) with the items you have to cellect. Then if you have collected one of them you have to delete the item from the array. Therefor you need to know which item you have collected and then set the array at the position of the item to null (or "" if you use strings). For example it could work like this:
private Actor[] collection = new Actor[] {...};//here you add instances of all the items you want to collect (e.g. new Actor1(), new Actor2(), ...);

public void act() {
    Actor collectedActor = getOneIntersectingObject(Actor.class);
    if (collectedActor != null) {
        deleteActorFromList(collectedActor);
    }
}

public void deleteActorFromList(Actor actor) {
    for (int i = 0; i < collection.length; i++) {
        if (collection[i] != null && actor.getClass().equals(collection[i].getClass())) {
            collection[i] = null;
        }
    }
}
Then you just have to write down all the items to collect in a GreenfootImage.
I.AM.A.LION<ROAR> I.AM.A.LION<ROAR>

2013/5/8

#
Thankyou:)
You need to login to post a reply.