【问题标题】:In Corona SDK, how do I limit the number of lines drawn to one?在 Corona SDK 中,如何将绘制的线数限制为一条?
【发布时间】:2014-10-18 17:54:15
【问题描述】:

如果我在绘制线条时将鼠标移出线条路径,则新线条会继续从同一中心点出现。我一直试图将它限制在一条线上,但我没有取得太大的成功。到目前为止,这是我的代码:

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)

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

【问题讨论】:

    标签: android ios lua cross-platform coronasdk


    【解决方案1】:

    据我了解,你想要的是这样的东西?

    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)
    

    享受吧!

    已编辑:代码。

    【讨论】:

    • 不,这不起作用。绘制另一条线后,这些线不会消失。
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 2021-02-09
    • 2014-02-20
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    相关资源
    最近更新 更多