newLine properties

Good morning,

local myLine = display.newLine ( [parent,] x1, y1, x2, y2)

creates a line joining the coordinates (x1, y1) and (x2, y2).
print (myLine.x) gives the x1 coordinate
and
print (myLine.y) gives the y1 coordinate

Is there a way to get x2 and y2 coordinates ?

[import]uid: 6661 topic_id: 26202 reply_id: 326202[/import]

No there is not, I’m afraid, and also the .x and .y parameters attached to the display object created by the newLine call is actually the centre position of the ‘line’. Try changing the display object’s .x and .y to 0,0. You’ll see it is off screen partially.

The reason is that the display object created by the newLine function does not behave as a vector object but as a regular display object, just like newImage or newRect do.

To change the position of the ends of the line you need to create a new line. To store the Ax,Ay,Bx and By values you need to literally store them on the object. Which, of course, is quite easy.

I recommend making a function which does that all for you - but of, that is basic code reuse.

I am working on a function which would allow the line object to behave the way you are wanting it to, because you’re not alone, but there are inherrant problems because of the many different ways lines (and their many possible vectors) can work. [import]uid: 8271 topic_id: 26202 reply_id: 106261[/import]

If it’s a straight line you can figure out the end points using object.contentsBounds API. This gives you the xMin, xMax, yMin, and yMax values of the object.

You can keep track of the end points by assigning custom properties to the line object.

myLine.xEnd = 40; myLine.yEnd = 100

You would assign these values when your first create the line and when you append to the line. They don’t do anything to the line object, just an easy way to keep track of the values. [import]uid: 7559 topic_id: 26202 reply_id: 106579[/import]