如何删除以前绘制的线?

时间:2012-04-16 18:38:55

标签: sdk line corona

以下是代码:

local physics = require "physics"
physics.start()

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
        print"began"
    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]:setColor(255, 255, 0)
            lines[i].width = 5

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

问题是:

  1. 例如:我绘制一条线,然后我绘制下一行,我希望删除前一行。我怎么能这样做?
  2. 我正在使用导演1.4,当我尝试重放级别时,这些行停止正确绘制,并且出现指向此行的错误 - if(lines[i]) then lineGroup:remove(i) end所以:我如何向{{{}添加行1}}并在重播级别时删除它们(我正在使用localGroup或正确更改场景?

1 个答案:

答案 0 :(得分:0)

嗯,我以为你想要多行,因为你把它们添加到一个行表中。如果您不需要多行,则只需存储1行。类似的东西:

local physics = require "physics"
physics.start()

local line
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
        if(line) then
            lineGroup:remove(1)
            line = nil
        end
        prevX = e.x
        prevY = e.y
        isDrawing = true
    elseif(e.phase == "moved") then
        local distance = distanceBetween(prevX, prevY, e.x, e.y)
        if(isDrawing and distance < 100) then
            if(line) then lineGroup:remove(1) end
            line = display.newLine(prevX, prevY, e.x, e.y)
            line:setColor(255, 255, 0)
            line.width = 5

            local dist_x = e.x - prevX
            local dist_y = e.y - prevY
            physics.addBody(line, "static", { density = 1, friction = 0.5, bounce = 1.6, shape = {0, 0, dist_x, dist_y, 0, 0} } )
            lineGroup:insert(line)
        end
    elseif(e.phase == "ended") then
        isDrawing = false
    end
end

Runtime:addEventListener("touch",drawLine)

要将它们添加到当前场景,我相信导演类会是这样的:

function scene:createScene( event )
    lineGroup = self.view
end

您只需将lineGroup设置为scene.view,而不是使用display.newGroup()

创建新组

要正确删除该行,您可以在退出场景功能中执行此操作:

function scene:exitScene( event )
    if(line) then
        lineGroup:remove(1)
        line = nil
    end
end

我建议您查看导演课程教程:http://www.youtube.com/watch?v=KudLE8h4kWw或此代码示例https://github.com/ansca/Storyboard-Sample