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

2014/4/28

How to draw a Line between two objects on my World

Rudolf Rudolf

2014/4/28

#
Hello again, new Problem, new Questions:( I´d like to know how to create, or draw a line between to objects automatically. If I have more than one object in my World I want to draw a line with klicking and drag to other Objects. Do anybody have an idea?
danpost danpost

2014/4/28

#
You will need an Actor object to be the line while you are drawing it (click-dragging from one object to another). Whether it is to stay an Actor once the line is completed or you draw the line on the background of the world afterwards is up to you (if you want each line to maintain a link between the two objects, keeping them as actor objects might be the way to go). You will need code in your world class act method to detect the dragging and create the object for the line. The class of the line object will then take over and while dragging continues, change size, location, and rotation to accommodate changes in the location of the mouse. You should have a boolean field in the class of the line to indicate the dragging state (whether done or not). When dragging ends, it should probably check to make sure it ends on another object or remove itself. You could have an array field of length two to hold the objects that are linked in the class of the line. The act method could change duties after the drag has ended to keeping between the two objects with the proper rotation (so if either or both linked objects move, the line will still connect them.
You need to login to post a reply.