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

2013/1/9

remove an actor at an certain place

seelensauger seelensauger

2013/1/9

#
I want to remove an subclass of an Actor which is added to the Wordl at an certain place:
getWorld().removeObject(this.getOneObjectAtOffset(getLocationX(),getLocationY(),null));
I made sure with a few tests: the Location is right this part (code) is called by the greenfoot system more infos: the class he sould find is a subbclass (rasemeaher) of a subclass of a subclass of actor all this subclasses have nothing special I think the problem will be that the code :
this.getOneObjectAtOffset(getLocationX(),getLocationY(),null
return an actor but there is no actor there is a subclass of actor. i hope i made myself understand. if you have any questions just ask I will try to answer, but try to use easy language because I'm german.
danpost danpost

2013/1/9

#
If the actor you are trying to remove is at a specific location:
if (!getWorld().getObjectsAt(getLocationX(), getLocationY(), rasemeaher.class).isEmpty())
{
    getWorld().removeObject(getWorld().getObjectsAt(getLocationX(), getLocationY(), rasemeaher.class).get(0));
}
should work. The problem with 'getObjectsAtOffset' is that 'offX' and 'offY' in
getObjectsAtOffset(offX, offY, null)
will be added to 'getX()' and 'getY()' to find the actual location to look for the object. Another way to look at it is that it would be equivalent to:
getWorld().getObjectsAt(getX()+offX, getY()+offY, null)
which is probably not what you want.
seelensauger seelensauger

2013/1/10

#
now the problem is i want to get every object which could be an that location. so nur just rasenmeaher.class (this one i just took to try it out) error message while compiling now: removeObject(greenfoot.Actor) cannot be applied to (java.lang.Object) the i have two questions: why get(0) at the end there? to get rid of an error message or so? what's this offX and offY I don't understand this part.
seelensauger seelensauger

2013/1/10

#
problem solved I look in thE Actor API what you meaned with offX offY. it was pretty easy because the object is at the ecatly same place like the mouse. so the solution is:
getWorld().removeObject(this.getOneObjectAtOffset(0,0,null));
thanks very much
seelensauger seelensauger

2013/1/10

#
i changed it sligthly so i can make class exeption which he shouldn't remove:
int ende = this.getObjectsAtOffset(0,0,null).size();
                    for (int i=0; i<ende;i++)
                    {
                        if(
                        this.getObjectsAtOffset(0,0,null).get(i).getClass() != RasenmaeherR.class
                        || 
                        this.getObjectsAtOffset(0,0,null).get(i).getClass() !=Grass.class
                        )
                        {
                            
                            getWorld().removeObject(this.getOneObjectAtOffset(0,0,
                            this.getObjectsAtOffset(0,0,null).get(i).getClass())
                            );
                        }
                    }
danpost danpost

2013/1/10

#
Using a 'for' loop to iterate though a list would be easier. Also, using 'instanceof' makes it more readable.
for (Object obj : getObjectsAtOffset(0, 0, null))
{
    Actor actor = (Actor) obj;
    if (actor instanceof RasenmaeherR || actor instanceof Grass) getWorld().removeObject(actor);
}
davmac davmac

2013/1/10

#
danpost, I think your suggested code does the opposite - it removes the object if it is an instance of RasenmaeherR or Grass. The code above it removes all objects that aren't an instance of one of those.
danpost danpost

2013/1/10

#
@davmac, you are so right. I missed the fact that those were '!=' and not '=='. Then the suggested code should have been:
for (Object obj : getObjectsAtOffset(0, 0, null))
{
    Actor actor = (Actor) obj;
    if (!(actor instanceof RasenmaeherR || actor instanceof Grass)) getWorld().removeObject(actor);
}
seelensauger seelensauger

2013/1/11

#
how exatly does this part funktion? why is it needed for?
 Actor actor = (Actor) obj;
seelensauger seelensauger

2013/1/11

#
my own code doesn't funktion always because when u delete one of the objects while the for is runnig the arraylist gets shorter and so the terminal give out an out of bounds exeption
seelensauger seelensauger

2013/1/11

#
my code at the moment:
                    for (Object obj : getObjectsAtOffset(0, 0, null))  
                    {  
                        Actor actor = (Actor) obj;  
                        if (!(actor instanceof Rasterobjekte || actor instanceof Grass || actor instanceof Vorschau))
                        {
                        getWorld().removeObject(actor);  
                        }
                    }
it does not remove the RasenmeaherR which should be remove. i think perhaps this has something to do with the fact that at the place where u should remove the RasenmeaherR is always a instanceof Vorschau and of instanceof Rasterobjekte, which perhaps makes the if for all three objects false
danpost danpost

2013/1/11

#
seelensauger wrote...
how exatly does this part funktion? why is it needed for?
 Actor actor = (Actor) obj;
Actor actor is as usual: declares a variable of type 'Actor', named 'actor' (Actor) obj takes the current 'Object' object (obj) and casts it to an 'Actor' type. This is needed to assign the value of 'obj' to 'actor', whose type is 'Actor' (cannot assign an object of type 'Object' to a field that holds an 'Actor' type; not directly). Actually, because we are not using any Actor class methods, we might get away without using this statement (coding this way has become a habit for me). Please list either what type objects you want removed or what type objects you do not want removed, depending on which list is shorter, specifying which list you give. Remember, if you give a type that has sub-classes, then those sub-classes are included in that type.
seelensauger seelensauger

2013/1/11

#
ok now i got it. the class I wanted to remove (RasenmaeherR) was a subtype of Rasterobjekte (i just needed these one to try out the positions). I changed that and now is everything ok. thanks a lot greetings seelensauger
You need to login to post a reply.