Corona SDK - 删除已绘制线条组,重新绘制。

我正在尝试使用小部件按钮删除任何我绘制的线,并允许玩家重新绘制。我尝试了多次删除组...它可以工作...但是当我重新开始绘制时,它会崩溃!有关此情况的任何帮助?

点赞
用户88888888
用户88888888

我修改了你的代码,现在它可以运行,但还不完美。你需要遵循自己的代码来精确地了解它的含义。虽然很奇怪,但当找不到你的对象时,Corona 会崩溃。

display.setStatusBar( display.HiddenStatusBar ) -- 隐藏状态栏
local physics = require "physics" -- 引入物理引擎模块
physics.start() -- 启动物理引擎

local widget = require( "widget" ) -- 引入 UI 组件模块

local lines = {} -- 存储线条的数组
local lineGroup = display.newGroup() -- 创建存储线条的组
local prevX,prevY -- 上一次手指所在的位置
local isDrawing = false -- 是否正在绘制线条
i = 0 -- 初始化线条数量为 0

-- 创建一个物体并添加到物理引擎中
local kittenCrate = display.newRect(10,10,25,25)
physics.addBody(kittenCrate, "dynamic", { density = 1, friction = 0.5, bounce = 1.6})

-- 计算两点之间的距离
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
        lineGroup.isVisible =true
    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, 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) -- 监听 touch 事件

-- 按钮事件处理函数
local function handleButtonEvent( event )
    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )
        i=0
        lineGroup.isVisible = false -- 隐藏存储线条的组
    end
end

-- 创建一个按钮
local button1 = widget.newButton
{
    left = 100,
    top = 200,
    id = "button1",
    label = "Remove Rifts",
    onEvent = handleButtonEvent
}
2015-03-10 07:52:32