【问题标题】:Corona SDK - Removing drawn line group and then drawing againCorona SDK - 删除绘制的线组,然后再次绘制
【发布时间】:2014-06-05 16:56:42
【问题描述】:

我正在尝试删除我使用小部件按钮绘制的所有线条,然后让玩家再次绘制。我曾多次尝试删除该组......并且它有效......但是当我再次绘制时它崩溃了!对这种情况有帮助吗?

display.setStatusBar( display.HiddenStatusBar )
local physics = require "physics"
physics.start()

local widget = require( "widget" )

local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 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
    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)




local function handleButtonEvent( event )

    if ( "ended" == event.phase ) then
        print( "Button was pressed and released" )

    end

end

-- Create the widget
local button1 = widget.newButton
{
    left = 100,
    top = 200,
    id = "button1",
    label = "Remove Rifts",
    onEvent = handleButtonEvent
}

【问题讨论】:

    标签: lua null drawing coronasdk


    【解决方案1】:

    我将您的代码更改为以下代码,它可以正常工作,但并不完美。你必须按照你的代码来看看它到底做了什么。虽然奇怪的是 Corona 在找不到您的对象时会崩溃。

    display.setStatusBar( display.HiddenStatusBar )
    local physics = require "physics"
    physics.start()
    
    local widget = require( "widget" )
    
    local lines = {}
    local lineGroup = display.newGroup()
    local prevX,prevY
    local isDrawing = false
    i = 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)
    
    
    
    
    local function handleButtonEvent( event )
    
        if ( "ended" == event.phase ) then
            print( "Button was pressed and released" )
    i=0
    lineGroup.isVisible = false
    
        end
    
    end
    
    -- Create the widget
    local button1 = widget.newButton
    {
        left = 100,
        top = 200,
        id = "button1",
        label = "Remove Rifts",
        onEvent = handleButtonEvent
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多