如何删除先前绘制的线? [英] How to delete a previously drawn line?

查看:158
本文介绍了如何删除先前绘制的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

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])然后lineGroup:remove(i)结束这样:如何将行添加到 localGroup 并重播级别时删除它们(我正在使用 director:changeScene( level1)或正确更改场景?

  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

您只需要将lineGroup设置为scene.view即可,而不是使用displa创建新的组y.newGroup()

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

我建议您看一下Director类的教程: http://www.youtube.com/watch?v=KudLE8h4kWw 或此代码示例 https://github.com/ansca/Storyboard-Sample

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

这篇关于如何删除先前绘制的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆