If I move the mouse off the line path when drawing a line, new lines continue appearing from the same center point. I have been trying to limit it to one line only, but I've not had much success. This is my code so far:

local lines = {}

local lineGroup = display.newGroup()

local prevX,prevY

local isDrawing = false

local i = 0

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
        lineGroup:removeSelf()
        lineGroup = nil
        lineGroup = display.newGroup()
        prevX = e.x
        prevY = e.y
        isDrawing = true
        i = i + 1
    elseif(e.phase == "moved") then
        local distance = distanceBetween(prevX, prevY, e.x, e.y)
        if(isDrawing and distance < 100) then
            --if(lines[i]) then lineGroup:remove(i) end
            lines[i] = display.newLine(prevX, prevY, e.x, e.y)
            lines[i]:setStrokeColor( 0,0,1 )
            lines[i].strokeWidth = 5

            local dist_x = e.x - prevX
            local dist_y = e.y - prevY
            physics.addBody(lines[i], "static", { density = 1, friction = .6, bounce = 2, 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)

If anyone could help, that would be great.

Related posts

Recent Viewed