簡體   English   中英

Corona SDK Lua,如何制作手指畫圖/繪畫應用程序?

[英]Corona SDK Lua, How do I make a finger drawing/painting app?

我對Lua相當陌生,並且編寫了以下代碼。

   display.setStatusBar(display.HiddenStatusBar)

--Create the 'brush'
function paint(event)
    locationX = event.x
    locationY = event.y
    brush = display.newCircle(locationX, locationY, 5)
end

Runtime:addEventListener("touch", paint)

只要單擊/按住鼠標,每次paint功能都會在屏幕上放置一個圓圈。 但是,我移動鼠標(使用Corona SDK)的速度越快,圓圈的間距就越大,並且會中斷直線的流動。

我怎樣才能改變它,使它畫出流暢的線條而不間斷?

根據您的代碼,每當觸發觸摸事件時,您要做的只是放一個圓圈(因此, display.newCircle )。 當您開始觸摸屏幕(在模擬器中單擊鼠標),在屏幕上移動手指以及取消單擊或將手指從屏幕上移開時,就會觸發觸摸事件

在您的情況下,您要在第一次觸摸屏幕,手指移動的位置以及手指/鼠標離開屏幕的位置放置一個5像素大小的圓圈。 您的問題來自手指移動階段或event.phase =“ moved”。 發生這種情況的原因是,根據您使用的硬件,您在移動過程中獲得的觸摸事件數量受到限制。 因此,如果移動足夠大,則將在放置的圓之間存在一些位置,由於該限制,您的觸摸事件或您的情況下的功能paint沒有被調用。

如果只需要一條線,則可以使用newLine方法代替newCircle方法。 您必須將觸摸輸入分為不同的階段,即“開始”,“移動”,“結束”。 在“開始”階段,您將啟動新行。 在“移動”或“結束”階段,您創建(使用換行功能),或添加到您現有的與線附加功能。

我尚未測試此代碼,但可能看起來像這樣:

local line                      --variable to hold the line object
local initX                     --initial X coordinate of touch
local initY                     --initial Y coordinate of touch
local lineCreated = false       --Flag to check if line is already created 

--Create the 'brush'
function paint(event)
    locationX = event.x
    locationY = event.y
    if event.phase == "began" then   --first touch
        --Delete previous line (in this case no multiple lines)
        if(line) then
            line:removeSelf()
            line = nil
        end

        --Set initX and initY with current touch location           
        initX = locationX       
        initY = locationY
    elseif event.phase == "moved" then   --during touch movement
        if lineCreated then
            --line has been created, just append to existing line
            line:append(locationX, locationY)
        else
            --Line has not been created
            --Make new line object, set color, and stroke width
            line = display.newLine(initX, initY, locationX, locationY)
            line:setStrokeColor( 0, 0, 1 )
            line.strokeWidth = 5

            --set line created flag to true
            lineCreated = true
        end     
    elseif event.phase == "ended" or event.phase == "cancelled" then --touch lifted
        --append last touch location to the line
        line:append(locationX, locationY)   
    end
    return true
end

Runtime:addEventListener("touch", paint)

這是一條基本線,因此拐角可能不平滑。 為了使線條平滑,您需要應用稍微復雜一些的算法,例如貝塞爾曲線。 關於其他編程語言,有很多討論(重要的是算法,當您更熟悉Lua時,可以輕松地將其用於Corona,Lua是一種相對容易學習的語言)。 您可以在此處獲得數學路徑,並且可以在此處找到有關Corona的資料。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM