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

2012/9/12

turn + direction

Moritz Moritz

2012/9/12

#
import greenfoot.*;  


public class Object extends other
{
    public int dx;
    public int dy;
   
    public Object() {
        dx = 4;
        dy = 0;
     
            }
  
    public void act() 
    {
     move();
     setLocation(getX()+dx, getY()+dy);
     turn(5);
  
    } 
    move() {
    Ho ho =(Ho) getOneIntersectingObject(Ho.class);
        if(ho ==null)
       {
          dx = -dx;
          
        }
        
    }
        
   
 }
  
The Object in this class is a block and it follows a line (ho.class) - always when it is in contact with the line the block moves with the speed of 4 - when it is not in contact with the line it changes the direction (dx = -dx) that is working... but the problem is I want the block to rotate while moving - and that´s why the block often stucks in the end of the line and doesn´t move anymore... how can I code that the block rotate while moving and change the direction when it is not in contact with the line? thank you =)
SPower SPower

2012/9/12

#
public void act()
{
    Ho ho = (Ho) getOneIntersectingObject(Ho.class);
    if (ho != null) { // move forward
         setLocation(getX() +dx, getY() +dy);
    } else { // rotate
         turn(5);
    }
}
I don't know what your move method is meant for, but put it somewhere in here.
Moritz Moritz

2012/9/15

#
when you code it this way the block follows the line - but it is not turning... only when the block isn´t in contact with the line it is turning... but I want the block to turn while moving...
danpost danpost

2012/9/15

#
If the object is to follow the line, keep it on the line (do not let it leave the line). That way, you will not have to deal with trying to get back to the line. See my Line Following Demo. Instead of making sure you are on the right color, make sure you are still on the Ho object.
You need to login to post a reply.