【问题标题】:In Corona SDK, how do I get one line to disappear when another is drawn?在 Corona SDK 中,如何在绘制另一条线时让一条线消失?
【发布时间】:2014-10-17 09:33:01
【问题描述】:

这是我目前的代码:

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
        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(1,45,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 = 0.5, 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】:

    如果您想在绘制新线时让 lineGroup 中的所有其他线消失,您可以简单地执行以下操作:

    评论你的行:

    if(lines[i]) then lineGroup:remove(i) end
    

    并在e.phase == "began"中添加以下行

    lineGroup:removeSelf()
    lineGroup = nil
    lineGroup = display.newGroup()
    

    继续编码............ :)

    【讨论】:

    • 非常感谢!这将极大地帮助我完成我的应用程序。
    猜你喜欢
    • 2014-10-18
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2020-11-09
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多