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

2019/7/27

Remove an actor when touching another

Fargesia Fargesia

2019/7/27

#
Hi! I have to do a pretty basic thing, but I can't get it working. Like I've got an actor who adds "carreVert" objects, and those object should delete themselves when touching some actors. Here's the code:
if(piece<=16){
            if(isTouching(pionBlanc.class)||isTouching(roiBlanc.class)||isTouching(tourBlanc.class)||isTouching(fouBlanc.class)||isTouching(reineBlanc.class)||isTouching(chevalBlanc.class)){
                getWorld().removeObject(this);
            }
        }
I want the object to delete himself if he's touching the" xBlanc.class" objects and his piece variable is under or equal to 16. But when I compile it I get this 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:714) As I understand it it deletes once and then can't find the object anymore because it's deleted wich leads to this error, but if it's deleted then it doesn't touch the" xBlanc.class" objects does it? Like I've got this code just up and it works just fine:
if(isAtEdge()==true){
            getWorld().removeObject(this);
        }
Why does it not work with the isTouching objects.class? Thank you!
Super_Hippo Super_Hippo

2019/7/28

#
It is not “deleted”, it is only removed from the world. The isTouching-method tries to get the location in the world (for example with getX) and this doesn't work because it isn't in a world. In your case, when you say you get this error when you compile it, it probably means that you placed the code into the constructor of the class where it doesn't belong to. Otherwise, it is often caused by removing the object in the act method and then trying to use a method which requires it to be in the world later. It would most likely be much easier to help you if you show the whole class code.
Fargesia Fargesia

2019/7/28

#
The whole class code looks like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class carreVert here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class carreVert extends Actor
{
    
    public static int piece, var=1;
    /**
     * Act - do whatever the carreVert wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage image = new GreenfootImage("carreVert.jpg");
        image.setTransparency(150);
        setImage(image);
        
        Plateau plateau=(Plateau) getWorld();
        
        if(isAtEdge()==true){
            getWorld().removeObject(this);
        }
        
        if(piece<=16){
            if(isTouching(pionBlanc.class)||isTouching(roiBlanc.class)||isTouching(tourBlanc.class)||isTouching(fouBlanc.class)||isTouching(reineBlanc.class)||isTouching(chevalBlanc.class)){
                getWorld().removeObject(this);
            }
        }
            
            
        
        
        if(piece>=17 && var==0){
            if(isTouching(pionNoir.class)||isTouching(roiNoir.class)||isTouching(tourNoir.class)||isTouching(fouNoir.class)||isTouching(reineNoir.class)||isTouching(chevalNoir.class)){
                getWorld().removeObject(this);
            }
            var++;
        }
        
         if(Greenfoot.mouseClicked(this)){
            if(piece==10){
            ((chevalBlanc)getWorld().getObjects(chevalBlanc.class).get(0)).bouger();
            }
            else if(piece==13){
            ((roiBlanc)getWorld().getObjects(roiBlanc.class).get(0)).bouger();
            }
            else if(piece==15){
            ((chevalBlanc)getWorld().getObjects(chevalBlanc.class).get(1)).bouger();
            }
            else if(piece==26){
            ((chevalNoir)getWorld().getObjects(chevalNoir.class).get(0)).bouger();
            }
            else if(piece==29){
            ((roiNoir)getWorld().getObjects(roiNoir.class).get(0)).bouger();
            }
            else if(piece==31){
            ((chevalNoir)getWorld().getObjects(chevalNoir.class).get(1)).bouger();
            }
            getWorld().removeObjects(getWorld().getObjects(carreVert.class));
            
            for(int i=0; i<=7; i++){
                for(int j=0;j<=7; j++){
                    System.out.print(Plateau.plateau[i][j]);
                    System.out.print(" ");
                }
                System.out.println("");
            }
        }
        
        
    }  
    
    
    
}
danpost danpost

2019/7/28

#
Lines 19 thru 21 should be in a constructor block (not in act). Line 23 can be removed (not later used). Add a return statement after lines 26, 31 and 40 (those places where you remove the actor from the world)..
Fargesia Fargesia

2019/7/28

#
Thanks danpost! Works just fine!
You need to login to post a reply.