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

2013/7/7

remove all objects of one class

kevkev kevkev

2013/7/7

#
Hallo, getWorld().removeObjects(getWorld().getObjects(P1.class)); don´t work, it don´t remove some of the P1.class pls help. I want to remove all objects from the class which are at the world.
Gevater_Tod4711 Gevater_Tod4711

2013/7/7

#
The code you used seems to be right to me. May some instances from other classes have the same image and so it looks like it didn't remove all objects of this class. But to fix this bug it would be helpfull to know the code you are using.
danpost danpost

2013/7/7

#
Are you sure that some of the remaining objects are of the P1 class? Right click on those objects you thought should have been removed and select 'Inspect' from the options. At the top of the new window will be the class that the object belongs to.
kevkev kevkev

2013/7/7

#
yes, i am sure. i use P1 p1 = new P1(); this.getWorld().addObject(p1, 10, 10); this.getWorld().addObject(this,10, 10); and inspect tells p1 : P1
danpost danpost

2013/7/7

#
You should not be using this at all
this.getWorld().addObject(this,10, 10);
and in what class is this code located?
kevkev kevkev

2013/7/7

#
it is in the actor how put it down. and the same actor have the code which dosnt work :D
Gevater_Tod4711 Gevater_Tod4711

2013/7/7

#
Could you post the whole code of this actor class? That would make it much easyer to help you.
kevkev kevkev

2013/7/7

#
ok but it is on german but it is a easy code :D
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Affe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Affe extends GifActor
{
    /**
     * gehört noch zu GifActor
     */
    public void addedToWorld(World world)
    {
        setImage("Actor.gif"); //You must use the setImage method and not just right click + setImage in the GUI.
    }
   
    
    /**
     * Steuerung und Weltgeschwindigkeit
     */
    public void act()
    {
        int punkte = 0;
        Greenfoot.setSpeed(50); //Setzt Welt auf 50% Speed
     
        super.act(); // Animation starten
        if(Greenfoot.isKeyDown("d")) // d gedrückt nach rechts gehen
        {
            move(1);
        }
        if(Greenfoot.isKeyDown("a")) // a gedrückz nach links gehen
        {
            move(-1);
        }
        if(Greenfoot.isKeyDown("w")) // w gedrückt nach oben gehen
        {
            setLocation(getX(),getY() -1);
        }

        if(Greenfoot.isKeyDown("s")) // s gedrückt nach unten gehen
        {
            setLocation(getX(),getY() +1);
        }
        if(bananeAufFeld())
        {
            bananeAufnehmen();
            punkte = punkte+1;
            bananeSpawn();
            
        }
        if(falleAufFeld())
        {
            falleAufnehmen();
            punkte = punkte-1;
        }
        if(punkte==1)
        {
            P1 p1 = new P1();
            this.getWorld().addObject(p1, 10, 10);
            this.getWorld().addObject(this,10, 10);
            P1 p11 = new P1();
            this.getWorld().addObject(p11, 10, 11);
            this.getWorld().addObject(this,10, 11);
            P1 p12 = new P1();
            this.getWorld().addObject(p12, 10, 12);
            this.getWorld().addObject(this,10, 12);
            P1 p13 = new P1();
            this.getWorld().addObject(p13, 10, 13);
            this.getWorld().addObject(this,10, 13);
            P1 p14 = new P1();
            this.getWorld().addObject(p14, 10, 14);
            this.getWorld().addObject(this,10, 14);
            P1 p15 = new P1();
            this.getWorld().addObject(p15, 10, 15);
            this.getWorld().addObject(this,10, 15);
            P1 p16 = new P1();
            this.getWorld().addObject(p16, 10, 16);
            this.getWorld().addObject(this,10, 16);
            P1 p17 = new P1();
            this.getWorld().addObject(p17, 11, 10);
            this.getWorld().addObject(this,11, 10);
            P1 p18 = new P1();
            this.getWorld().addObject(p18, 12, 10);
            this.getWorld().addObject(this,12, 10);
            P1 p19 = new P1();
            this.getWorld().addObject(p19, 13, 10);
            this.getWorld().addObject(this,13, 10);
            P1 p20 = new P1();
            this.getWorld().addObject(p20, 14, 10);
            this.getWorld().addObject(this,14, 10);
            P1 p21 = new P1();
            this.getWorld().addObject(p21, 14, 11);
            this.getWorld().addObject(this,14, 11);
            P1 p22 = new P1();
            this.getWorld().addObject(p22, 14, 12);
            this.getWorld().addObject(this,14, 12);
            P1 p23 = new P1();
            this.getWorld().addObject(p23, 14, 13);
            this.getWorld().addObject(this,14, 13);
            P1 p24 = new P1();
            this.getWorld().addObject(p24, 14, 14);
            this.getWorld().addObject(this,14, 14);
            P1 p25 = new P1();
            this.getWorld().addObject(p25, 14, 15);
            this.getWorld().addObject(this,14, 16);
            P1 p26 = new P1();
            this.getWorld().addObject(p26, 14, 16);
            this.getWorld().addObject(this,14, 16);
            P1 p27 = new P1();
            this.getWorld().addObject(p27, 13, 16);
            this.getWorld().addObject(this,13, 16);
            P1 p28 = new P1();
            this.getWorld().addObject(p28, 12, 16);
            this.getWorld().addObject(this,12, 16);
            P1 p29 = new P1();
            this.getWorld().addObject(p29, 11, 16);
            this.getWorld().addObject(this,11, 16);
            P1 p30 = new P1();
            this.getWorld().addObject(p30, 11, 11);
            this.getWorld().addObject(this,11, 11);
            P1 p31 = new P1();
            this.getWorld().addObject(p31, 11, 12);
            this.getWorld().addObject(this,11, 12);
            P1 p32 = new P1();
            this.getWorld().addObject(p32, 11, 13);
            this.getWorld().addObject(this,11, 13);
            P1 p34 = new P1();
            this.getWorld().addObject(p34, 11, 14);
            this.getWorld().addObject(this,11, 14);
            P1 p35 = new P1();
            this.getWorld().addObject(p35, 11, 15);
            this.getWorld().addObject(this,11, 15);
            P1 p36 = new P1();
            this.getWorld().addObject(p36, 12, 11);
            this.getWorld().addObject(this,12, 11);
            P1 p37 = new P1();
            this.getWorld().addObject(p37, 12, 15);
            this.getWorld().addObject(this,12, 15);
            P1 p40 = new P1();
            this.getWorld().addObject(p40, 13, 11);
            this.getWorld().addObject(this,13, 11);
            P1 p41 = new P1();
            this.getWorld().addObject(p41, 13, 12);
            this.getWorld().addObject(this,11, 12);
            P1 p42 = new P1();
            this.getWorld().addObject(p42, 13, 13);
            this.getWorld().addObject(this,11, 13);
            P1 p43 = new P1();
            this.getWorld().addObject(p43, 13, 14);
            this.getWorld().addObject(this,11, 14);
            P1 p44 = new P1();
            this.getWorld().addObject(p44, 13, 15);
            this.getWorld().addObject(this,11, 15);
            P2 i1 = new P2();
            this.getWorld().addObject(i1, 12, 12);
            this.getWorld().addObject(this,12, 12);
            P2 i2 = new P2();
            this.getWorld().addObject(i2, 12, 13);
            this.getWorld().addObject(this,12, 13);
            P2 i3 = new P2();
            this.getWorld().addObject(i3, 12, 14);
            this.getWorld().addObject(this,12, 14);
        }
        if(punkte==2)
        {
            getWorld().removeObjects(getWorld().getObjects(P1.class));
            getWorld().removeObjects(getWorld().getObjects(P2.class)); 
        }
        
        if(jägerAufFeld())
        {
            Affe aktAffe = (Affe)this.getOneObjectAtOffset(0, 0, Affe.class);
            if(aktAffe != null)
            {
                setLocation(65,40);
                this.getWorld().removeObject(aktAffe);
                setImage("GameOver.jpg"); 
            }
        }
        
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/7/7

#
Why do you use this.getWorld().addObject(this,10, 10) ? I think if the actor is already in the world that either sets his location or does nothing. In both cases it isn't the best idea to use this like danpost already said. But I don't see why the actors P1 and P2 aren't removed. Probably they are removed and new ones are set into the world at the same time so it looks like they aren't removed. Could you post the code for the methods bananeAufFeld, bananeAufheben, falleAufFeld, ...? May the bug is to be found there.
danpost danpost

2013/7/7

#
Yeah, every act method the value of 'punkte' is reset to zero. I do not think you want that. You should declare that field outside of the method.
kevkev kevkev

2013/7/7

#
for exampel, the others are all the same
 public boolean bananeAufFeld()
    {
        if (this.getOneObjectAtOffset(0, 0, Banane.class)!= null)
        {
           return true;     
        } 
        else
        {
            return false;   
        }
    }
 public void bananeAufnehmen()
    {
        Banane aktBanane = (Banane)this.getOneObjectAtOffset(0, 0, Banane.class);
        if(aktBanane != null)
        {
            this.getWorld().removeObject(aktBanane);
        }
        else
        {
            System.out.println("Hier ist keine Banane!");   
        }
     }
kevkev kevkev

2013/7/7

#
Yeah, every act method the value of 'punkte' is reset to zero. I do not think you want that. You should declare that field outside of the method.
that can be the problem^^ but in which part it must be than?
danpost danpost

2013/7/7

#
int punkte = 0;

public void act()
{ ...
kevkev kevkev

2013/7/7

#
so now it works^^ thanks for help!
You need to login to post a reply.