How can I access the x and y of each point on a line?

I’m making a simple prototype which draws a line between 2 objects which can be dragged around.

When each object is dragged I’d like each point on the line to update it’s position so that it matches the objects. My line will only ever have 2 points.

How do I access the x and y of each point on the line, once it has been created?

I’ve provided some drag and drop code below.

\_H = display.contentHeight;  
\_W = display.contentWidth;  
  
local function point1()  
 point1 = display.newRect (0,0,30,30)  
 point1:setFillColor(0,255,0)  
 point1.x = \_W / 2 - 100  
 point1.y = \_H / 2  
  
 function dragPlayer(event)  
 point1.x = event.x  
 point1.y = event.y  
 end  
 point1:addEventListener( "touch", dragPlayer )  
end  
  
local function point2()  
 point2 = display.newRect (0,0,30,30)  
 point2:setFillColor(0,255,0)  
 point2.x = \_W / 2   
 point2.y = \_H / 2  
  
 function dragPlayer(event)  
 point2.x = event.x  
 point2.y = event.y  
 end  
 point2:addEventListener( "touch", dragPlayer )  
end  
  
local function laserB()  
 local barrier = display.newLine(point1.x,point1.y,point2.x,point2.y)  
 barrier:setColor(0,0,255)  
 barrier.width = 5   
end  
  
local function lineMove()  
 --what do I put here to access the x and y of each point on line?  
end  
   
local function startGame()  
 point1()  
 point2()  
 laserB()  
end  
  
startGame()  

Thanks in advance.

Dan [import]uid: 67933 topic_id: 24047 reply_id: 324047[/import]

create a table and insert the x, y for each pt. then traverse the table and modify accordingly.

c
[import]uid: 24 topic_id: 24047 reply_id: 96979[/import]

Hey Carlos, thanks for taking the time to respond. I really appreciate it.

It’s not the changing values I’m having trouble with. As suggested I’ve created a table, entered the values, and modify them each frame.

The part I can’t seem to wrap my head around is how to feed that info back to the line object without destroying it and creating a new one every frame?

local function laserB()  
  
 local pT = {}  
  
 local function lineMove()  
 table.insert (pT, 1, point1.x)  
 table.insert (pT, 2, point1.y)  
 table.insert (pT, 3, point2.x)  
 table.insert (pT, 4, point2.y)  
 print(point1.x) -- to check the values are changing  
 end  
 Runtime:addEventListener("enterFrame", lineMove)  
  
 local barrier = display.newLine(point1.x,point1.y,point2.x,point2.y)  
 barrier:setColor(0,0,255)  
 barrier.width = 5   
 return pT  
end  

I’m sure it’s some simple twist of logic, but for the life of me I can’t work it out.

Any further assistance appreciated.

Dan [import]uid: 67933 topic_id: 24047 reply_id: 97044[/import]

Hello,

I’m in the same case.
spider_newgent, did you just solve this issue?

Any idea?

TIA.

Alberto. [import]uid: 44013 topic_id: 24047 reply_id: 112287[/import]

@albert1

Here is one solution to the above, can be of course tweeked and optimized…:wink:

— Copy and Paste > Plug-n-Play —

\_H = display.contentHeight;  
\_W = display.contentWidth;  
local lineGroup = display.newGroup();  
  
local barrier;  
  
local point1 = display.newRect (0,0,30,30);  
point1:setFillColor(0,255,0);  
point1.x = \_W / 2 - 100;  
point1.y = \_H / 2;  
lineGroup:insert(point1);  
point1:toFront();  
  
local point2 = display.newRect (0,0,30,30);  
point2:setFillColor(255,0,0);  
point2.x = \_W / 2;   
point2.y = \_H / 2;  
lineGroup:insert(point2);  
point2:toFront();  
  
local line = { p1X, p1Y, p2X, p2Y };  
  
local barrier = display.newLine(point1.x,point1.y,point2.x,point2.y);  
barrier:setColor(0,0,255);  
barrier.width = 5;   
  
line.p1X = point1.x;  
line.p1Y = point1.y;  
line.p2X = point2.x;  
line.p2Y = point2.y;  
lineGroup:insert(barrier);  
barrier:toBack();  
  
local function lineMove()  
  
  
 barrier:toBack();  
 barrier = display.newLine( line.p1X, line.p1Y, line.p2X, line.p2Y );  
 barrier:setColor(0,0,255);  
 barrier.width = 5;  
  
  
 return true;  
  
end  
local function point1\_func()  
  
  
   
 function dragPlayer(event)  
  
 barrier:setColor(0,0,0);  
 point1.x = event.x;  
 point1.y = event.y;  
 line.p1X = point1.x;  
 line.p1Y = point1.y;  
  
 point1:toFront();  
  
 display.getCurrentStage():setFocus(event.target);  
  
 lineMove();  
  
 if event.phase == "ended" or event.phase == "cancelled" then  
  
 display.getCurrentStage():setFocus( nil );  
  
 end  
  
 end  
  
 point1:addEventListener( "touch", dragPlayer );  
  
 return true;  
end  
   
   
local function point2\_func()  
  
  
  
 function dragPlayer(event)  
  
 barrier:setColor(0,0,0);  
 point2.x = event.x;  
 point2.y = event.y;  
 line.p2X = point2.x;  
 line.p2Y = point2.y;  
  
 point2:toFront();  
  
 display.getCurrentStage():setFocus(event.target);  
  
 lineMove();  
  
 if event.phase == "ended" or event.phase == "cancelled" then  
  
 display.getCurrentStage():setFocus( nil );  
  
 end  
  
 end  
  
 point2:addEventListener( "touch", dragPlayer );  
  
 return true;  
end  
   
   
local function startGame()  
  
 point1\_func();  
 point2\_func();  
  
end  
   
startGame();  

Have fun with it…:wink:
Larry [import]uid: 107633 topic_id: 24047 reply_id: 112339[/import]