0

这是代码:

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. 我正在使用director 1.4,当我尝试重播关卡时,线条停止正确绘制并且出现指向这条线的错误 -if(lines[i]) then lineGroup:remove(i) end所以:如何localGroup在重播关卡时添加线条并删除它们(我正在使用director:changeScene("level1")或正确改变场景?
4

1 回答 1

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

于 2012-04-16T19:50:17.007 回答