Lua 容器中的电晕拖放瓦片
我以前也问过类似的问题,但我需要一些帮助来解决它。我喜欢拼字图,我可以在我的容器中拖放,但我复制了很多代码。我还想知道如何在任意数量的容器中而不是在一个特定的容器中放置瓷砖。下面是一段代码。有人能帮忙吗,因为我想避免重复代码,我需要解决如何在多个容器上放置一个平铺。Edit希望每个容器都能阅读字母并组合成一个完整的单词Lua 容器中的电晕拖放瓦片,lua,coronasdk,Lua,Coronasdk,我以前也问过类似的问题,但我需要一些帮助来解决它。我喜欢拼字图,我可以在我的容器中拖放,但我复制了很多代码。我还想知道如何在任意数量的容器中而不是在一个特定的容器中放置瓷砖。下面是一段代码。有人能帮忙吗,因为我想避免重复代码,我需要解决如何在多个容器上放置一个平铺。Edit希望每个容器都能阅读字母并组合成一个完整的单词 display.setStatusBar( display.HiddenStatusBar ) local posX, posY = 100, 200 local sizeX,
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)
display.setStatusBar(display.HiddenStatusBar)
局部posX,posY=100200
本地sizeX,sizeY=40,40
局部位置X1,位置Y1=150200
局部posx,posy=10,50
本地容器=display.newRoundedRect(posX、posY、sizeX、sizeY、3)
容器:setFillColor(0,0255)
container.strokeWidth=3
容器:setStrokeColor(100100100)
本地容器2=display.newRoundedRect(posX1、posY1、sizeX、sizeY、3)
容器:setFillColor(0,0255)
容器2.strokeWidth=3
容器2:设置行程颜色(100100100)
本地myObject=display.newImage(“字母/B.png”,150,10)
局部myObject2=display.newImage(“字母/a.png”,10,10)
局部myObject3=display.newImage(“字母/C.png”,200,10)
--触摸监听功能
功能myObject:触摸(事件)
如果event.phase==“开始”,则
self.markX=self.x——存储对象的x位置
self.markY=self.y——存储对象的y位置
elseif event.phase==“移动”然后
本地x=(event.x-event.xStart)+self.markX
本地y=(event.y-event.yStart)+self.markY
self.x,self.y=x,y——根据上述计算移动对象
如果((x>=((sizeX/2)+posX-((2/3)*sizeX)))和(y>=((sizeY/2)+posY-((1/3)*sizeY)))和(x=((sizeY/2)+posY-((2/3)*sizeY)))和(x=((sizeY/2)+posY-((1/3)*sizeY)))和(x=((sizeY/2)+posY-((2/3)+sizeY=(2)*和(sizeY)/posY=(x=)和(sizeY=((2)*((sizeY/2)+posY1-((1/3)*sizeY)))和(x=((sizeY/2)+posY1-((2/3)*sizeY)))和(x=((sizeY/2)+posY1-((1/2)*sizeY)))和(x=((sizeY/2)+posY1-((2/3)*sizeY)))和(x=((sizeY/2)+posY1-((1/2)*sizeY)和(x=(1/2)*sizeY=(2)+和(sizeY=(x=)和(sizeY=(2)+sizeY=(2)*(2/3)*SiZy)和(x=((SiZe/ 2)+Pyy1-((1/2)*SiZy))和(x=(SiZe/2)+Pyy1-((1/3)*SiZy))和(x=((SiZe/2)+Pyy1-((2/3)*SiZy))和(x=((SiZe/2)+Pyy1-((1/2)*SiZy))和(X我会考虑使用表)。您需要在表中有所有的瓦片,所以您正在使用。+POSIY1-(
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是您的磁贴,并在容器上循环,这样您就可以对所有这些容器进行测试,这样您就可以使用单触式处理程序函数