This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kutariyo
Kutariyo wrote ...

2014/6/29

Need a command for Greenfoot, have one but that doesn't work

1
2
Kutariyo Kutariyo

2014/6/29

#
Hey guys, I have to create a game with Greenfoot for school. It's about a cat which has to catch birds to get through a meadow to finally bring a cookie monster a cookie. I'm pretty far with that game but now I need an command. This command should cause that when the cat ate a bird (in game named Börd) a randomly choosen wall (in game named Wand) should disappear. I tried to do this with a for-loop, but no syntax errors were shown and also the command didn't work. Could you tell me what I've done wrong? My command was: /** * Act - do whatever the Cooookieee wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Wand aktWand = (Wand)this.getOneObjectAtOffset(0, 0, Wand.class); if (BördAufFeld () == true) { BördAufnehmen (); for (int Zauber=0; Zauber<Greenfoot.getRandomNumber (40); Zauber++) { getWorld().removeObject(aktWand); } } Cooookieee is the cat in the game. BördAufnehmen means that the cat eats the bird. BörAufFeld means that a bird is on the cat's field. Zauber just is a variable. I'm very grateful for all your help. Greetings Kutariyo
danpost danpost

2014/6/29

#
You have set 'aktWand' to a specific Wand object (if any are intersecting the center of the cat) and your 'removeObject' statement is removing that object (if any) from the world one time. All other iterations of the loop are just attempts to remove that same Wand object (if any) from the world. If you want to randomly choose one of the Wand objects that are in the world, you will need to randomly choose one from a list of all Wand objects in the world. This means using 'getWorld().getObjects(Wand.class)' instead of 'getOneObjectAtOffset(0, 0, Wand.class)', The 'size' of the returned List object will be needed to determine the random range to choose from and 'get(int)' will be needed to refer to the one randomly chosen. Refer to the List interface API for more info on these methods.
Kutariyo Kutariyo

2014/6/29

#
First thanks for your help. Does this mean I have to write these 'size' and 'get(int)' into my command? If yes, how do I have to write them in the command because we just started to use Greenfoot for the first year in school and we never spoke about something like 'size'?
Kutariyo Kutariyo

2014/6/29

#
Maybe you could give me an example because I really don't know how to use this in my command. By the way I wrote this command into the the cat's class.
danpost danpost

2014/6/29

#
I would presume by your response that you are unfamiliar with Java, as well. All methods are preceded by an object, like in the following:
1
objectReference.methodName(/* arguments */);
When you do not see an object mentioned, it is because 'this.', which is the default object that methods are executed for, was removed. For example, when you write in your subclass of World, this:
1
addObject(new ActorClassName(), 0, 0);
you are actually saying:
1
this.addObject(new ActorClassName(), 0, 0);
where 'this' refers to the world object that the actor is being added into. 'addObject' is a non-static method that can be found in the World class API. This means it must be executed on a World object. 'removeObject' is another World class method. From an actor class, because 'this' will refer to the actor, we need to run the method on a World object explicitly -- 'getWorld().removeObject(actor);' (which is the same as 'this.getWorld().removeObject(actor);'). Notice what is happening here: a) 'this' refers to an Actor object b) 'getWorld' (an Actor class method) returns a World object c) 'removeObject' (a World class method) removes the given actor from the world The same general path can be realized for getting the size of or an element from a List object: a) 'this' refers to an Actor object b) 'getWorld' (an Actor class method) returns a World object (the world the actor is in) c) 'getObjects' (a World class method) returns a List object (objects in the world on the given class) d) 'size' (a List class method) returns the size of the List object and 'get' (a List class method) returns an element from the List object You will also need 'getRandomNumber(int)' from the Greenfoot class API for choosing a random element from the List object. It is a 'static' method and is not run on an object of the class, but on the class itself:
1
int randomNumber = Greenfoot.getRandomNumber(size);
Kutariyo Kutariyo

2014/6/29

#
But how do I have to use this 'size'?
Kutariyo Kutariyo

2014/6/29

#
Is it this 40 behind the 'getRandomNumber'?
danpost danpost

2014/6/29

#
Kutariyo wrote...
Is it this 40 behind the 'getRandomNumber'?
I cannot say what the '40' is. I can only say that using it as the argument in the 'getRandomNumber' method call will return a number between 0 and 39, inclusive. Other than that, I do not know where you got that number from or why it might be important.
But how do I have to use this 'size'?
1
int wandCount = getWorld().getObjects(Wand.class).size();
will give you the number of Wand objects in the world. You will probably want to use 'wandCount' instead of '40' -- and you should not be using a 'for' loop at all.
Kutariyo Kutariyo

2014/6/29

#
danpost wrote...
Kutariyo wrote...
and you should not be using a 'for' loop at all.
So what should I use then?
danpost danpost

2014/6/29

#
Kutariyo wrote...
< Quote Omitted > So what should I use then?
You do not need to iterate through the list; just get one random element from it.
1
2
3
int wandCount = getWorld().getObjects(Wand.class).size();
int randomWand = Greenfoot.getRandomNumber(wandCount);
Object wand = getWorld().getObjects(Wand.class).get(randomWand);
Line 1 determines how many Wand object are currently in the world. Line 2 randomly picks a number between '0' and 'wandCount-1', inclusive. Line 3 gets the randomly chosen Wand object from the list of Wand objects currently in the world.
Kutariyo Kutariyo

2014/6/29

#
:'( I now totally feel so stupid. I try everything but at least I don't understand. I feel so hopeless because you are trying so hard to explain me but my brain doesn't want to understand and I already have to present my game in school tomorrow...I felt so good I came so far with my game and now this.....Now I'm crying the whole time and I'm feeling totally confused...I'm very thankful for the time you spend on explaining me all these things but I don't even know anymore where to write these things and how to use it and at least how to build up these command... :'(
Kutariyo Kutariyo

2014/6/29

#
All I wanted was to write im the cat's class that when the cat eats a bird, the bird disappears (I already have this) and that a randomly chosen wall removes so that there's a free place afterwards.
danpost danpost

2014/6/29

#
Just replace your 'for' loop with the above 3 lines, with a line to remove 'wand' from the world.
Kutariyo Kutariyo

2014/6/29

#
So now I have: if (BördAufFeld () == true) { BördAufnehmen (); int wandCount = getWorld().getObjects(Wand.class).size(); int randomWand = Greenfoot.getRandomNumber(wandCount); Object wand = getWorld().getObjects(Wand.class).get(randomWand); this.getWorld().removeObject(aktWand); } No syntax errors but no wand disappears...
danpost danpost

2014/6/29

#
Remove the following line:
1
Wand aktWand = (Wand)this.getOneObjectAtOffset(0, 0, Wand.class);
and change
1
2
3
this.getWorld().removeObject(aktWand);
// to this
getWorld().removeObject(wand);
There are more replies on the next page.
1
2