Skip to content Skip to sidebar Skip to footer

In Corona Sdk, How Do I Limit The Number Of Lines Drawn To One?

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 h

Solution 1:

as I understood what you want is something like this?

local physics = require("physics");

local lineGroup = display.newGroup()

local currentLine = nil;

local prevX,prevY


localfunctiondistanceBetween(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
endlocalfunctionredrawLine(x1, y1, x2, y2)if (currentLine) then
        currentLine:removeSelf();
    end

    currentLine = display.newLine(x1, y1, x2, y2);
    currentLine:setStrokeColor(0,0,1);
    currentLine.strokeWidth = 5;
endlocalfunctiondrawLine(e)if(e.phase == "began") then

        prevX = e.x
        prevY = e.y

        if (currentLine) then
            currentLine:removeSelf();
            currentLine = nil;
        endelseif(e.phase == "moved") thenlocal distance = distanceBetween(prevX, prevY, e.x, e.y)
        if(distance < 100) then 
            redrawLine(prevX, prevY, e.x, e.y);
        endelseif(e.phase == "ended") thenif (currentLine) thenlocal dist_x = e.x - prevX
            local dist_y = e.y - prevY
            physics.addBody(currentLine, "static", { density = 1, friction = .6, bounce = 2, shape = {0, 0, dist_x, dist_y, 0, 0} } )
            lineGroup:insert(currentLine);
        endendend

Runtime:addEventListener("touch",drawLine)

Enjoy!

Edited: code.

Post a Comment for "In Corona Sdk, How Do I Limit The Number Of Lines Drawn To One?"