set a maximum line lenght

HI guys, I’m using the following code for let players draw a line during the game, he question is: how can I set a maximum lenght for that line?

here’s the code:

local lines = {}  
local line\_number = 1  
local line\_width = 10  
local prev\_x, prev\_y, ball  
   
local function draw\_line(e)  
   
 if e.phase == "began" then  
 prev\_x = e.x  
 prev\_y = e.y  
   
 elseif e.phase == "moved" then  
 lines[line\_number] = display.newLine(prev\_x, prev\_y, e.x, e.y)   
 lines[line\_number]:setColor(math.random(255), math.random(255), math.random(255))  
 lines[line\_number].width = line\_width  
 dist\_x = e.x - prev\_x  
 dist\_y = e.y - prev\_y  
 -- Add a physics body that's a flat polygon that follows each segment of the line  
 physics.addBody(lines[line\_number], "static", { density = 1, friction = 0.5, bounce = -1.0, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 prev\_x = e.x  
 prev\_y = e.y  
 line\_number = line\_number + 1  
 elseif e.phase == "ended" then  
 end  
end  
   
   
new\_ball\_button = display.newRect(10, HEIGHT - 75, 150, 75)  
delete\_lines\_button = display.newRect(WIDTH - 150, HEIGHT - 75, 150, 75)  
   
   
   
 --create walls  
 local leftWall = display.newRect(0, 0, 1, display.contentHeight)  
local rightWall = display.newRect(display.contentWidth, 0, 1, display.contentHeight)  
local ceiling = display.newRect(0, 0, display.contentWidth, 1)  
  
physics.addBody(leftWall, "static", {bounce = 0.1})  
physics.addBody(rightWall, "static", {bounce = 0.1})  
physics.addBody(ceiling, "static", {bounce = 0.1})  
   
   
   
   
-- Create a new ball  
function new\_ball\_button:touch(e)  
 if e.phase == "ended" then  
 ball = display.newCircle(50, 50, 25)   
 physics.addBody(ball, {density = 5000, friction = 0.5, bounce = 1.0, radius = 25} )  
 ball:setFillColor(255,0,0)  
 end  
end  
   
-- Delete all existing lines  
function delete\_lines\_button:touch(e)  
   
 if e.phase == "ended" then  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 lines = {}  
 line\_number = 1  
 end  
end  
   
  
   
Runtime:addEventListener("touch", draw\_line)  
new\_ball\_button:addEventListener("touch", new\_ball\_button)  
delete\_lines\_button:addEventListener("touch", delete\_lines\_button)  

and then, how can how delete the line when the ball(contained in this code, you can run it) hit the line? thanks [import]uid: 76800 topic_id: 24045 reply_id: 324045[/import]

set your max line lenght

get the distance of the line using the distance formula
d = sqrt ( (x1 - x) ^2 + ( y1 - y) ^2 )

if the distance is greater than max length then stop drawing line.

hope this helps…

c
[import]uid: 24 topic_id: 24045 reply_id: 96982[/import]

Thanks a lot, but I haven’t solved my problem, can you post me the correct code please?

[import]uid: 76800 topic_id: 24045 reply_id: 97105[/import]