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

2012/6/15

intersects help

TimbooXD TimbooXD

2012/6/15

#
public void act() 
    {
        int beginX = getX();
        int beginY = getY(); 
        
         wallnon thewall = null;    
        thewall = (wallnon) getOneIntersectingObject(wallnon.class);    
        if (thewall != null)    
            {
            setLocation(beginX, beginY);    
             turned();
           move(1);
        }
           
           
        
        else {
            move(2);
        }
    }    
I am trying to let the object turn random but when it hits the wall it intersects with the center of the object and it needs to intersect to the outside of the image.Because now it can clitch easy out of the object. the world
public class wereld extends World
{

    /**
     * Constructor for objects of class wereld.
     * 
     */
    public wereld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(23, 23, 25);   
        int  poswallx = 1;
         addObject(new wallnon(),5,11);
         addObject(new wallnon(),7,13);
         addObject(new wallnon(),15,13);
        addObject(new wallnon(),7,9);
    
       addObject(new wallnon(),9,7);
       addObject(new wallnon(),11,5);
       addObject(new wallnon(),13,7);
        addObject(new wallnon(),9,13);
        addObject(new wallnon(),15,9);
        addObject(new wallnon(),17,11);
         addObject(new wallnon(),9,15);
           addObject(new wallnon(),13,15);
             addObject(new wallnon(),11,17);
        
        

        do
        {
            addObject(new wallnon(),poswallx,1);
            addObject(new wallnon(),poswallx,5);
            addObject(new wallnon(),poswallx,9);
            addObject(new wallnon(),poswallx,13);
            addObject(new wallnon(),poswallx,17);
            addObject(new wallnon(),poswallx,21);
         
            poswallx = poswallx +4;
           
        } while (poswallx <=21);
    }
}
the wallnon object doesnt have code. please help me.
erdelf erdelf

2012/6/15

#
public void act()   
        {  
            int beginX = getX();  
            int beginY = getY();   
              
             wallnon thewall = null;      
            thewall = (wallnon) getOneIntersectingObject(wallnon.class);      
            List walls = getObjectsInRange(2, wallnon.class);        
            Iterator i = walls.iterator();
            while(i.hasNext()) 
            {
                 wallnon thewall = (wallnon) i.next();
                  if(thewall != null) break;
            }
            if (thewall != null)      
                {  
                setLocation(beginX, beginY);      
                 turned();  
               move(1);  
            }  else 
            {  
                move(2);  
            }  
        }      
TimbooXD TimbooXD

2012/6/15

#
iterator doesnt work it gives a erorr
TimbooXD TimbooXD

2012/6/15

#
the iterator error isn't there anymore but now it errort on line 12. thewall error(variable thewall is already defined in method act())
danpost danpost

2012/6/16

#
The problem with your originally posted code, is that you are checking for intersection before moving, which is not what you want. What you need to do is: 1) save x and y 2) move 3) check intersect 4) if intersecting, move back to (x,y) and turn
TimbooXD TimbooXD

2012/6/16

#
it works thanks
You need to login to post a reply.