Android 在Corona SDK中,如何将绘制的线数限制为一条?

Android 在Corona SDK中,如何将绘制的线数限制为一条?,android,ios,lua,cross-platform,coronasdk,Android,Ios,Lua,Cross Platform,Coronasdk,如果在绘制直线时将鼠标移离直线路径,则新直线将继续从同一中心点显示。我一直试图把它限制在一行,但我没有太多的成功。这是我目前的代码: 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 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)
localline={}
本地线组=display.newGroup()
本地prevX,prevY
本地isDrawing=false
局部i=0
(x1,y1,x2,y2)之间的局部函数距离
局部距离x=x2-x1
本地区y=y2-y1
本地distanceBetween=math.sqrt((dist_x*dist_x)+(dist_y*dist_y))
返回距离
结束
局部功能抽绳(e)
如果(e.阶段==“开始”),则
线组:removeSelf()
线组=零
lineGroup=display.newGroup()
prevX=e.x
prevY=e.y
isDrawing=true
i=i+1
elseif(e.phase==“moved”),然后
本地距离=距离(prevX,prevY,e.x,e.y)
如果(isDrawing和距离<100),则
--如果是(行[i]),则线组:删除(i)结束
行[i]=显示.换行符(prevX,prevY,e.x,e.y)
行[i]:设置行程颜色(0,0,1)
行[i]。冲程宽度=5
本地区x=e.x-prevX
本地区y=e.y-上一个
addBody(线[i],“静态”,{density=1,摩擦力=6,反弹=2,形状={0,0,dist_x,dist_y,0,0})
线组:插入(线[i])
结束
elseif(e.phase==“结束”),然后
isDrawing=false
结束
结束
运行时:addEventListener(“触摸”,抽绳)

如果有人能帮忙,那太好了。

我知道你想要的是这样的东西

local physics = require("physics");

local lineGroup = display.newGroup()

local currentLine = nil;

local prevX,prevY


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 redrawLine(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;
end

local function drawLine(e)
    if(e.phase == "began") then

        prevX = e.x
        prevY = e.y

        if (currentLine) then
            currentLine:removeSelf();
            currentLine = nil;
        end

    elseif(e.phase == "moved") then
        local distance = distanceBetween(prevX, prevY, e.x, e.y)
        if(distance < 100) then 
            redrawLine(prevX, prevY, e.x, e.y);
        end
    elseif(e.phase == "ended") then

        if (currentLine) then
            local 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);
        end
    end
end

Runtime:addEventListener("touch",drawLine)
本地物理=要求(“物理”);
本地线组=display.newGroup()
本地电流线=零;
本地prevX,prevY
(x1,y1,x2,y2)之间的局部函数距离
局部距离x=x2-x1
本地区y=y2-y1
本地distanceBetween=math.sqrt((dist_x*dist_x)+(dist_y*dist_y))
返回距离
结束
局部函数重绘线(x1、y1、x2、y2)
如果(当前线路),则
currentLine:removeSelf();
结束
currentLine=显示换行符(x1,y1,x2,y2);
currentLine:setStrokeColor(0,0,1);
currentLine.strokeWidth=5;
结束
局部功能抽绳(e)
如果(e.阶段==“开始”),则
prevX=e.x
prevY=e.y
如果(当前线路),则
currentLine:removeSelf();
currentLine=nil;
结束
elseif(e.phase==“moved”),然后
本地距离=距离(prevX,prevY,e.x,e.y)
如果(距离<100),则
重绘线(prevX,prevY,e.x,e.y);
结束
elseif(e.phase==“结束”),然后
如果(当前线路),则
本地区x=e.x-prevX
本地区y=e.y-上一个
addBody(currentLine,“static”{density=1,摩擦力=6,反弹=2,形状={0,0,dist_x,dist_y,0,0})
线组:插入(currentLine);
结束
结束
结束
运行时:addEventListener(“触摸”,抽绳)
享受吧


已编辑:代码。

不,这不起作用。在绘制另一条线后,这些线不会消失。