Cannot draw a line

Hi, I used the following code to let players draw a line -but- it won’t work:

local lines = {}  
local lineGroup = display.newGroup()  
local prevX,prevY  
local isDrawing = false  
local i = 1  
  
   
local function distanceBetween(x1, y1, x2, y2)  
 local dist\_x = x2 - x1  
 local dist\_y = y2 - y1  
 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y))  
 return distanceBetween  
end  
   
local function drawLine(e)  
 if(e.phase == "began") 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  
  
  
 prevX = e.x  
 prevY = e.y  
 isDrawing = true  
   
 elseif(e.phase == "moved") then  
 local distance = distanceBetween(prevX, prevY, e.x, e.y)  
 if(isDrawing and distance \< 50) then  
 if(lines[i]) then lineGroup:remove(i) end  
 lines[i] = display.newLine(prevX, prevY, e.x, e.y)  
 lines[i]:setColor(255, 255, 0)  
 lines[i].width = 2  
 lines[i].myName = "lines"  
   
 local dist\_x = e.x - prevX  
 local dist\_y = e.y - prevY  
 physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 lineGroup:insert(lines[i])  
 end  
  
  
 elseif(e.phase == "ended") then  
 isDrawing = false  
 end  
end  
Runtime:addEventListener("touch",drawLine)  

so how can I fix my problem?

Thanks :smiley:
[import]uid: 122056 topic_id: 34933 reply_id: 334933[/import]

What isn’t working exactly? Do you get any errors?

I ran that code, with the addition of requiring and starting the physics library, and it worked ok.

As it is, the longest line you can draw is 50 pixels because of the ‘distance < 50’ instruction, removing that means a line can be drawn of any length. [import]uid: 93133 topic_id: 34933 reply_id: 138809[/import]

Thanks, for your answers, anyway, the original code was a little bit different, but I did some change to it (i.e “distance<50”).
Unfortunately I don’t get any error in the corona terminal.

Anyway, I my original post, I wrote a previous version of the code, the code I’m actually using is:

local lines = {}  
local lineGroup = display.newGroup()  
local prevX,prevY  
local isDrawing = false  
local i = 1  
  
   
local function distanceBetween(x1, y1, x2, y2)  
 local dist\_x = x2 - x1  
 local dist\_y = y2 - y1  
 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y))  
 return distanceBetween  
end  
   
local function drawLine(e)  
 if(e.phase == "began") 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  
  
  
 prevX = e.x  
 prevY = e.y  
 isDrawing = true  
   
 elseif(e.phase == "moved") then  
 local distance = distanceBetween(prevX, prevY, e.x, e.y)  
 if(isDrawing and distance \< 50) then  
 if(lines[i]) then lineGroup:remove(i) end  
 lines[i] = display.newLine(prevX, prevY, e.x, e.y)  
 lines[i]:setColor(255, 255, 0)  
 lines[i].width = 2  
 lines[i].myName = "lines"  
   
 if(lines[i].y \< 400) then  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 end  
   
 local dist\_x = e.x - prevX  
 local dist\_y = e.y - prevY  
 physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 lineGroup:insert(lines[i])  
 end  
  
  
 elseif(e.phase == "ended") then  
 isDrawing = false  
 end  
end  
Runtime:addEventListener("touch",drawLine)  

uh, and even:

if(lines[i].y \< 400) then  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 end  

has been written by me [import]uid: 122056 topic_id: 34933 reply_id: 138811[/import]

So that bit is meant to delete any lines which are drawn above y = 400? So you can only draw lines in the bottom 20 pixels of the screen (depending on what your config.lua is setup as).

I got errors which I fixed by moving this section to the bottom of the function, otherwise you are trying to add physics to lines and insert lines that may not exist once this has run:

[lua]if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
lines[i].parent:remove(lines[i])
lines[i] = nil
end
end
end[/lua]

[import]uid: 93133 topic_id: 34933 reply_id: 138815[/import]

exactly, I’ve inserted that code because I would like to delete all lines above y = 400.

Anyway, the code still not working :frowning: and it is strange, even because if I use the director class instead of the storyboard, it works without any problems.

This time I used this:

[code]
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 1

local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end

local function drawLine(e)
if(e.phase == “began”) 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

prevX = e.x
prevY = e.y
isDrawing = true

elseif(e.phase == “moved”) then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 50) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 2
lines[i].myName = “lines”

local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], “static”, { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end

elseif(e.phase == “ended”) then
isDrawing = false
end

if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
lines[i].parent:remove(lines[i])
lines[i] = nil
end
end
end

end

Runtime:addEventListener(“touch”,drawLine)
[/code] [import]uid: 122056 topic_id: 34933 reply_id: 138845[/import]

What isn’t working exactly? Do you get any errors?

I ran that code, with the addition of requiring and starting the physics library, and it worked ok.

As it is, the longest line you can draw is 50 pixels because of the ‘distance < 50’ instruction, removing that means a line can be drawn of any length. [import]uid: 93133 topic_id: 34933 reply_id: 138809[/import]

Thanks, for your answers, anyway, the original code was a little bit different, but I did some change to it (i.e “distance<50”).
Unfortunately I don’t get any error in the corona terminal.

Anyway, I my original post, I wrote a previous version of the code, the code I’m actually using is:

local lines = {}  
local lineGroup = display.newGroup()  
local prevX,prevY  
local isDrawing = false  
local i = 1  
  
   
local function distanceBetween(x1, y1, x2, y2)  
 local dist\_x = x2 - x1  
 local dist\_y = y2 - y1  
 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y))  
 return distanceBetween  
end  
   
local function drawLine(e)  
 if(e.phase == "began") 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  
  
  
 prevX = e.x  
 prevY = e.y  
 isDrawing = true  
   
 elseif(e.phase == "moved") then  
 local distance = distanceBetween(prevX, prevY, e.x, e.y)  
 if(isDrawing and distance \< 50) then  
 if(lines[i]) then lineGroup:remove(i) end  
 lines[i] = display.newLine(prevX, prevY, e.x, e.y)  
 lines[i]:setColor(255, 255, 0)  
 lines[i].width = 2  
 lines[i].myName = "lines"  
   
 if(lines[i].y \< 400) then  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 end  
   
 local dist\_x = e.x - prevX  
 local dist\_y = e.y - prevY  
 physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist\_x, dist\_y, 0, 0} } )  
 lineGroup:insert(lines[i])  
 end  
  
  
 elseif(e.phase == "ended") then  
 isDrawing = false  
 end  
end  
Runtime:addEventListener("touch",drawLine)  

uh, and even:

if(lines[i].y \< 400) then  
 for i = #lines, 1, -1 do  
 if (lines[i]) then  
 lines[i].parent:remove(lines[i])  
 lines[i] = nil  
 end  
 end  
 end  

has been written by me [import]uid: 122056 topic_id: 34933 reply_id: 138811[/import]

So that bit is meant to delete any lines which are drawn above y = 400? So you can only draw lines in the bottom 20 pixels of the screen (depending on what your config.lua is setup as).

I got errors which I fixed by moving this section to the bottom of the function, otherwise you are trying to add physics to lines and insert lines that may not exist once this has run:

[lua]if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
lines[i].parent:remove(lines[i])
lines[i] = nil
end
end
end[/lua]

[import]uid: 93133 topic_id: 34933 reply_id: 138815[/import]

exactly, I’ve inserted that code because I would like to delete all lines above y = 400.

Anyway, the code still not working :frowning: and it is strange, even because if I use the director class instead of the storyboard, it works without any problems.

This time I used this:

[code]
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 1

local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end

local function drawLine(e)
if(e.phase == “began”) 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

prevX = e.x
prevY = e.y
isDrawing = true

elseif(e.phase == “moved”) then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 50) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 2
lines[i].myName = “lines”

local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], “static”, { density = 1, friction = 0.5, bounce = -0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end

elseif(e.phase == “ended”) then
isDrawing = false
end

if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
lines[i].parent:remove(lines[i])
lines[i] = nil
end
end
end

end

Runtime:addEventListener(“touch”,drawLine)
[/code] [import]uid: 122056 topic_id: 34933 reply_id: 138845[/import]