如何设置绘制线的最大长度?(Corona SDK)
问题描述:
我目前正在研究一些项目,它包含一个要绘制的线的选项。但我通常不能设置最大行长。我怎样才能做到这一点?如何设置绘制线的最大长度?(Corona SDK)
这里是距离公式的代码(我认为):
local function distanceBetween(e, prev)
local distanceBetween = 0
local dist_x = e.x - prevX ; local dist_y = e.y - prevY;
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end
这里是一行代码:
local lines = {}
local myLines = {}
local prevX,prevY
local i = 1
--prevX = x1, prevY = y1, e.x = x2, e.y = y2
local function drawLine(e)
distanceBetween = false
if(e.phase == "began") then
myLines[i] = {}
prevX = e.x
prevY = e.y
elseif(e.phase == "moved")then
if prevX then
myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y)
myLines[i][#myLines[i]]:setColor(255,255,0)
myLines[i][#myLines[i]].width = 5
myLines[i][#myLines[i]].alpha=1
myLines[i][#myLines[i]].myName = "Line"
dist_x = e.x - prevX
dist_y = e.y - prevY
physics.addBody(myLines[i][#myLines[i]], "static", { density = 1, friction = 0.5, bounce = 1.6, shape = {0, 0, dist_x, dist_y, 0, 0} })
lineGroup:insert(myLines[i][#myLines[i]])
distanceBetween = true
if (distanceBetween > 50) then
drawLine("ended")
end
end -- this is how i think line should be maximized
prevX = e.x
prevY = e.y
elseif(e.phase == "ended")then
prevX = nil
prevY = nil
i = i + 1
removeLine(myLines[#myLines-1])
end
end
Runtime:addEventListener("touch",drawLine);
答
编辑:这工作得很好,我电晕应用程序。这是你想要做的吗?
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
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)
嗨,谢谢!其实我正是在想如何绘制直线而不是弯曲的线条......你能帮忙吗?那真是太棒了! – barmyman 2012-04-10 17:15:41
加上你的代码给了我一个奇怪的错误:试图对本地'x2'(一个零值) – barmyman 2012-04-10 17:48:23
执行算术哦,似乎通过将x1,x2,y1,y2 = 0作为本地值来修复它,但是这是正确的吗?现在我有这个错误:尝试调用upvalue'distanceBetween'(一个数字值) 真的希望你会有所帮助! – barmyman 2012-04-10 18:00:36