How to delete a previously drawn line?

自闭症网瘾萝莉.ら 提交于 2019-12-08 09:21:25

问题


Here is the code:

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)

The questions are:

  1. For example: I draw a line, then I draw the next line and I want the previous line to be deleted. How can I do this?
  2. I am using director 1.4 and when I am trying to replay level, the lines stop to draw correctly and the error appears that is pointing to this line - if(lines[i]) then lineGroup:remove(i) end so: How can I add lines to the localGroup and delete them when replaying level (I am using director:changeScene("level1") or changing scene correctly?

回答1:


Hmm, I assumed you wanted multiple lines because you was adding them to a lines table. If you don't need multiple lines, you can just store the 1 line. Something like:

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)

To add them to the current scene, I believe the director class would be something like this:

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

You just need to set the lineGroup to the scene.view instead of creating a new group with display.newGroup()

To properly remove the line, you can do it in the exit scene function:

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

I'd recommend taking a look at the director class tutorials: http://www.youtube.com/watch?v=KudLE8h4kWw or this code sample https://github.com/ansca/Storyboard-Sample



来源:https://stackoverflow.com/questions/10179714/how-to-delete-a-previously-drawn-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!