0

我以前问过类似的问题,但我需要一些帮助来清理它。我的图像像拼字游戏瓷砖,我可以将其拖放到我的容器中,但我复制了很多代码。我还想知道如何能够在任意数量的容器中而不是一个特定的容器中放置图块。这是代码片段。有人可以帮忙吗,因为我想避免重复代码,我需要弄清楚如何在多个容器上放置一个图块。编辑希望每个容器都读取字母并组合成一个完整的单词

display.setStatusBar( display.HiddenStatusBar )

local posX, posY = 100, 200
local sizeX, sizeY = 40, 40
local posX1, posY1 = 150, 200
local posx, posy = 10, 50

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

local container2 = display.newRoundedRect( posX1, posY1, sizeX, sizeY, 3 )
container:setFillColor( 0,0,255 )
container2.strokeWidth = 3
container2:setStrokeColor(100, 100, 100)



local myObject = display.newImage("letters/B.png" , 150, 10)
local myObject2 = display.newImage("letters/a.png" , 10, 10)
local myObject3 = display.newImage("letters/C.png" , 200, 10)

-- 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

        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( 0,0,255 )
        else
            container:setFillColor( 0,0,255 )
        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);
        end

    end

    return true
end

---------------------------------------------------------------------------
--------------------------test---------------------------------------------
---------------------------------------------------------------------------
-- touch listener function
function myObject2: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
        if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
            container2:setFillColor( 0,0,255 )
        else
            container2:setFillColor( 0,0,255 )
        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) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
            self.x, self.y = posX1 + (sizeX/2), posY1 + (sizeY/2);
        end

    end

    return true
end
------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function myObject3: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
        if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
            container2:setFillColor( 0,0,255 )
        else
            container2:setFillColor( 0,0,255 )
        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) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
            self.x, self.y = posX1 + (sizeX/2), posY1 + (sizeY/2);

        end

    end

    return true
end

myObject:addEventListener( "touch", myObject )
myObject3:addEventListener( "touch", myObject3 )
myObject2:addEventListener( "touch", myObject2)
4

1 回答 1

1

我会考虑为此使用表格。您需要将所有图块放在一个表中,因此您正在使用 myObject[1] myObject[2]

等等然后你可以使用一个单一的触摸函数,其中 event.target 是被触摸的对象(即,如果有人触摸了 myObject[2],它将在你的函数中作为 event.target 引用。那么如果你的容器也在一个表中:

container[1]
container[2]

然后在您的触摸处理程序中,您可以执行如下循环:

for i = 1, #container do
      -- do your code to check to see if your object is in your container
end

您应该能够使用 event.target 是您的 tile 和循环容器这一事实的组合来摆脱单个触摸处理程序函数,以便您可以针对所有这些进行测试。

于 2013-01-06T02:44:02.407 回答