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

2012/3/14

Lines

Razzo Razzo

2012/3/14

#
I want to create a line that is created by the movement of an Actor, like, a "pen down" tool, That draws a line in the path of the moving actor. How can I do this‽
Morran Morran

2012/3/14

#
Well, there are two different ways to do this. One way is to draw directly onto the World as a canvas, and the other one is to add Actors that have a particular image but don't do much. To be honest, I've never drawn onto the screen before, so I'll let someone who has answer that part. But, I can explain how to trail Actors to create images. Try something like this:
public void act()
{
   //after your normal code....
   //add this call to addInk()
   addInk();
}

//then you define addInk()
public void addInk()
{
   if(hasMoved()) //check if has moved to not add Ink twice in same place.
      getWorld().addObject(new Ink(), getX(), getY());
}

//hasMoved you'd define here...
public void hasMoved()
{
   boolean moved = (lastX != getX() && lastY != getY());
   //you'd have to define lastX and lastY as "int"s at the top of your class
   lastX = getX();
   lastY = getY();
   return moved;
}
The Ink class would then look something like this:
public class Ink extends Actor
{
   private int frame; //to add in animation.

   public Ink()
   {
       setImage(whateverImageYouWant);
       frame = 0;
   }

   public void act()
   {
       //if you don't care for the animation, just don't have anything in the act method.
       blot();
   }

   public void blot()
   {
       //this will just make the image fill out over time, as opposed to staying constant.
       if(frame < 5)   {
          getImage().scale(frame*4 + 1, frame*4 + 1);
          frame++;
       }
   }
}
You probably want to have the "whateverImageYouWant" just be a simple circle image, but you can try experimenting with other images. Trailing Actors is more versatile but slows your program down more. I hope this helps.
Razzo Razzo

2012/3/14

#
Okay thanks., I have the line drawn fine now, But how can I detect that it is there, it is too "thin" to detect using getColorat (They are a distict color so this method should work okay) Because im using move(3), and they are 1 pixel wide, I could speed up the simulation time, but how could I do it without doing that?
Morran Morran

2012/3/14

#
You could make the images wider, or you could drop 3 per act. You could try:
move();
addInk();
move();
addInk();
move();
addInk();
instead of
move(3);
addInk();
I hope that this helps.
You need to login to post a reply.