以下是代码:
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)
问题是:
if(lines[i]) then lineGroup:remove(i) end
所以:我如何向{{{}添加行1}}并在重播级别时删除它们(我正在使用localGroup
或正确更改场景?答案 0 :(得分: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