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

2012/6/20

Remove.Objects ohne Spielstop

1
2
Spielkind Spielkind

2012/6/20

#
Schönen guten Abend, ich versuche es erstmal auf deutsch. Für ein Geburtstagsgeschenk wollten meine Tochter und ich ein kleines Scenario kreieren. Sie hatte Greenfoot auf dem Zukunftstag in Niedersachsen kennengelernt, mir war es völlig unbekannt (bin kein Programmierer, nur versierter Anwender). Jetzt fehlt uns langsam die Zeit und ich finde keine Lösung. Problem: aufbauend auf der HamsterWorld wir haben 4 Schildkröten erstellt, die Erdbeeren ablegen. Wenn sie keine mehr im Maul haben, sollen sie a) entweder weiterlaufen, ohne weiter Erdbeeren abzulegen, b) sich im Kreis drehen (void turn), oder c) sich mit "Pop" auflösen. Das Auflösen kriege ich noch hin, aber leider bleibt dann die ganze Welt stehen. Turn habe ich nicht zum Laufen gekriegt; a) ebenfalls nicht (dann läuft die Schildkröte weiter, und legt nach einer Pause ab -1 weiterhin Erdbeern ab). Mag uns jemand helfen? Danke! Hier die wichtigsten Ausschnitte aus dem Code der Schildkroete.class: public void act() { if (vornFrei()) { vor(); } else { linksUm(); if (vornFrei()) { vor(); } else { linksUm(); linksUm(); vor(); } } if (ErdbeereDa()) { nimm(); } else { gib(); } } UND: public void gib() throws MaulLeerException { if (maulLeer()) { getWorld().removeObject(this); Greenfoot.playSound("pop.wav"); } ErdbeerenImMaul--; getWorld().addObject(new Erdbeere(), getX(), getY()); Edit: Oh, ich fand eine erste Lösung bei "Actors death" Nur eine Zeile mit "return;" nach playSound, und schon funzt es. Wenn mir trotzdem einer zeigen könnte, wie die anderen beiden Varianten gehen könnten, würde ich mich seehhr freuen, danke!
danpost danpost

2012/6/20

#
Miene Deutch ist nicht gut.
public void gib() throws MaulLeerException {
    World w = getWorld(); // *************
        if (maulLeer()) {
            getWorld().removeObject(this); 
           // ******* getWorld() = null ******** Object eine Welt nicht haben.
            Greenfoot.playSound("pop.wav");
        }
        
        ErdbeerenImMaul--;
        w.addObject(new Erdbeere(), getX(), getY()); // ************
Spielkind Spielkind

2012/6/20

#
Hallo Danpost, still online though ist late? thanks for your reply, but the line w.addObject(new Erdbeere(), getX(), getY()); // ************ courses an error: "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663)" I think I stay at the return-command I describet below until tomorrow and goto bed now. CU
danpost danpost

2012/6/20

#
Sorry,
public void gib() throws MaulLeerException {
    World w = getWorld();
    int x = getX(), y = getY(); // ***********
    if (maulLeer()) {
        getWorld().removeObject(this);
        Greenfoot.playSound("pop.wav");
    }
    ErdbeerenImMaul--;
    w.addObject(new Erdbeere(), x, y); // *************
Spielkind Spielkind

2012/6/21

#
Hallo again. I still have a little problem. I wanted do end my Scenario with a sound, When all turtles are removed. I've got in the schildkroete.class: /** * liefert die Gesamtzahl an existierenden Schildkroeten im * Territorium * * @return die Gesamtzahl an existierenden Schildkroeten im * Territorium */ public int getAnzahlSchildkroete() { return getWorld().getObjects(Schildkroete.class).size(); } So I tried something like: Actor schildkroete; schildkroete = (getWorld().getObjects(Schildkroete.class)); if (schildkroete = 0) { Greenfoot.playSound("happy-birthday.wav"); Greenfoot.stop(); } But it always stops with an error like incompatible types, or incompatible type, required: variable, found value I'm not firm with this. Could you please help me? Where is the mistake?
erdelf erdelf

2012/6/21

#
if(schildkroete == null) {
when you write only one of =, you set something, with double =, you get the value
Spielkind Spielkind

2012/6/21

#
Halle erdelf, thanx for your reply, your right. Unfortunatly there it is still an error in the line: schildkroete=(getWorld().getObjects(Schildkroete.class)); Greenfoot response: incompatible types I don't understand, what he wants me to do. schildkroete.class is an int. What means ".size()" after "return getWorld().getObjects(Schildkroete.class)" in the "public int getAnzahlSchildkroete()" ?
danpost danpost

2012/6/21

#
Instead of: Actor schildkroete; schildkroete = (getWorld().getObjects(Schildkroete.class)); if (schildkroete = 0) { try this: if (!getWorld().getObjects(Schildkroete.class).isEmpty()) { 'getObjects' returns a List object that never will be 'null', but it may be an empty list!
Spielkind Spielkind

2012/6/21

#
I'm so sorry, danpost, but this won't work. Now he plays the sound, but from the start though there are 4 turtles in the world. You seem to be right but something still is wrong. Are your sure with the list of getObjects? If I rightclick on a turtle and choose "int getAnzahlSchildkreote()" I become the answer int: 4. So there should be also possible a null. I nearly get crazy about this. Tomorrow afternoon it should be ready and I "just wanted to add" some nice singing at the end of our scenario ;(
erdelf erdelf

2012/6/21

#
try this:
if(numberOfObjects() == 1) // ersetz das hier einfach mit der Anzahl der sonstigen Objekte in der Welt
{
      Greenfoot.playSound("happy-birthday.wav"); 
      Greenfoot.stop();
}
danpost danpost

2012/6/21

#
My mistake. Remove '!' from the statement.
Spielkind Spielkind

2012/6/22

#
@ danpost Don't worry, greenfoot pointed out, that the "!" is swrong. I removed, but than he stops after the first turtle was removed with this error: java.lang.NullPointerException at Schildkroete.act(Schildkroete.java:98) (witch is the line with your code) Do you understand why? @ erdelf: I tried that out; first looked good. Unfortunatedly the Compilation ended with the error: "cannot find symbol - method numberOfObjects()" if I add (getWorld() -> if (getWorld().numberOfObjects() == 204) { Greenfoot.playSound("happy-birthday.wav"); it will sound, but: before it stops after each turtle that is removed and hte wave come before the last turtle is removed (what means, there are enough objects in the world) I 'll try and have to think about some more... thanx to you for your help, please please continue There are two questions in my mind: 1) is it possible to add "public void act" to the world? 2) how can I tell greenfoot to play the sound just onetime, not on and on and on and...
erdelf erdelf

2012/6/22

#
1) yes 2) use a boolean, normally set false, not in the method.
sound = false;
and change the method to this:
    
if(!sound)
{
     if(getWorld().numberOfObjects() == 1) 
    {  
          Greenfoot.playSound("happy-birthday.wav");   
          Greenfoot.stop();  
          sound = true;
    }
}  
oh and when you write a code in discussion use the button code under the reply box, a window will open and write your code in it, so it can be read easier. You can see it in my reply here, look how my code is showed and how yours.
Spielkind Spielkind

2012/6/22

#
Oh thanx, I wondered how you get this good looking code in! Let my repeat: I put your code in schildkroete.class, public void act() and compile? Theres's still an error:
        if(!sound)
cannot find symbol - variable sound
And I think about the number of Objects: because the turtles mostly give strawberrys, but sometimes they eat strawberrys, my end amount of Objects (= 204) could arrive before the last turtle is removed; for this I can't stop greenfoot (but ... I think, this will not really be a problem; when the last turtle is away, nothing more is happening than the happy-birthday-sound..., that will be okay.) to 1) I was thinking about to start with the sound and let the turtle run meanwile; than I don't have the need of any "get.Numbers" or so. but I can't stop greenfoot, to get only onetime the sound. Isn't it possible to give geenfoot a number, how often to play the sound?
erdelf erdelf

2012/6/22

#
well,
boolean sound = false;
should be outside of act();
There are more replies on the next page.
1
2