0
我目前正在研究一些項目,它包含一個要繪製的線的選項。但我通常不能設置最大行長。我怎樣才能做到這一點?如何設置繪製線的最大長度?(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);
嗨,謝謝!其實我正是在想如何繪製直線而不是彎曲的線條......你能幫忙嗎?那真是太棒了! – 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