2013-01-03 79 views
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) 

回答

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 

Yo你應該能夠通過使用事件event.target這個事實的組合,並且在容器上循環來測試所有的容器,從而獲得單一的觸摸處理函數。