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

2013/3/30

Self Touching

JetLennit JetLennit

2013/3/30

#
How do i make it to where if an object touches another instance of its self it will remove i of the objects
danpost danpost

2013/3/30

#
Same way as if it is touching another type object.
if (!getIntersectingObjects(getClass()).isEmpty()) getWorld().removeObject(this);
Of course, 'getClass()' can be replaced by the actual class (ex. 'Worm.class').
JetLennit JetLennit

2013/3/30

#
I had tried that....... it comes up with
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.
in an error message
danpost danpost

2013/3/30

#
Then you do not have this line in the act method or a method it calls (unless you have executed 'getWorld().removeObject(this)' prior to this line).
JetLennit JetLennit

2013/3/30

#
it is in the act method this is in the entire class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class dumpster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class dumpster extends coming
{
    /**
     * Act - do whatever the dumpster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        if(this.getX() < 5)
        {
          getWorld().removeObject(this);
        }    
        if (!getIntersectingObjects(getClass()).isEmpty()) getWorld().removeObject(this);  
    }    
}
danpost danpost

2013/3/30

#
danpost wrote...
(unless you have executed 'getWorld().removeObject(this)' prior to this line).
Line 20 show this is indeed the case. A better act method would be:
public void act()
{
    move();
    getWorld().removeObjects(getIntersectingObjects(dumpster.class));
    if (getX() < 5) getWorld().removeObject(this);
}
JetLennit JetLennit

2013/3/30

#
oops... duh! i cant believe i didn't notice that!!!
JetLennit JetLennit

2013/3/30

#
It has worked brilliantly
You need to login to post a reply.