2

我是 Corona SDK 的新手。

我正在为应用程序使用拖放实现测试。

我为一个“吸引”它附近的拖动对象的容器实现了下面的代码。

display.setStatusBar( display.HiddenStatusBar )

local posX, posY = 100, 200
local sizeX, sizeY = 150, 100

local container = display.newRect( posX, posY, sizeX, sizeY )
container:setFillColor( 255,0,0 )
container.strokeWidth = 3
container:setStrokeColor(100, 100, 100)

local myObject = display.newRect( 0, 0, sizeX, sizeY )
myObject.alpha = 0.6
myObject:setFillColor( 0,0,255 )


-- dcenter, d1, d2, d3, d4 are test points: IF THERE ARE AT LEAST 2 POINTS INSIDE THE TARGET CONTAINER, THAN THE OBJECT WILL BE ATRACTED INTO THE CONTAINER
-- uncomment the code bellow to see the test points

-- local dcenter = display.newCircle( (sizeX/2) - 1, (sizeY/2) - 1, 2 )
-- dcenter:setFillColor(120,120,120)
-- 
-- local d1 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 )
-- d1:setFillColor(120,120,120)
-- 
-- local d2 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 )
-- d2:setFillColor(120,120,120)
-- 
-- local d3 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 )
-- d3:setFillColor(120,120,120)
-- 
-- local d4 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 )
-- d4:setFillColor(120,120,120)


-- touch listener function
function myObject:touch( event )
    if event.phase == "began" then

        self.markX = self.x    -- store x location of object
        self.markY = self.y    -- store y location of object

    elseif event.phase == "moved" then

        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY

        self.x, self.y = x, y    -- move object based on calculations above

        -- uncomment the code bellow to see the test points (following myObject)
        -- dcenter.x, dcenter.y = x, y
        -- d1.x, d1.y = x - (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2)
        -- d2.x, d2.y = x - (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2)
        -- d3.x, d3.y = x + (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2)
        -- d4.x, d4.y = x + (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2)

        if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
            container:setFillColor( 100,0,0 )
        else
            container:setFillColor( 255,0,0 )
        end

    elseif event.phase == "ended" then

        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY

        -- main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it's 1/3 in the target and 1 area that atract it when it's 1/4 in the target
        if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
            self.x, self.y = posX + (sizeX/2), posY + (sizeY/2);

            -- uncomment the code bellow to see the test points (following myObject)
            -- dcenter.x, dcenter.y = posX + (sizeX/2), posY + (sizeY/2)
            -- d1.x, d1.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2)
            -- d2.x, d2.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2)
            -- d3.x, d3.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2)
            -- d4.x, d4.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2)
        end

    end

    return true
end


myObject:addEventListener( "touch", myObject )

我只想知道是否已经有一些 api。

4

1 回答 1

1

不,它没有 API。

另外我建议您使用焦点系统,因此在拖动您的对象时不要与其他对象的“触摸”事件进行交互...

例子:

local function myTouchListener(event)
    if event.phase == "began" then
        display.getCurrentStage( ):setFocus( event.target );
    elseif event.phase == "ended" then
        display.getCurrentStage( ):setFocus( nil );
    end
end
于 2012-08-15T20:52:58.900 回答