我有多个对象和多个容器,我的计划是任何对象都可以放在任何容器上,但是一旦容器上放有一个对象,我希望它被占用,不再允许任何对象进入它。是否有一个isOcuppied电话或类似的东西,我找不到任何这方面的例子。
local function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
-- Spurious events can be sent to the target, e.g. the user presses
-- elsewhere on the screen and then moves the finger over the target.
-- To prevent this, we add this flag. Only when it's true will "move"
-- events be sent to the target.
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
t.xScale = 1.7
t.yScale = 1.7
t.x = event.x - t.x0
t.y = event.y - t.y0
--t1 = t1..t.value
elseif "ended" == phase or "cancelled" == phase then
for i = 1, #posX do
if (((t.x >= ((sizeX/2) + posX[i] - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX[i] - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX[i] - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX[i] + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
t.x, t.y = posX[i] + (sizeX/2), posY + (sizeY/2);
elseif (((t.x >= ((sizeX/2) + posX1[i] - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX1[i] - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX1[i] - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX1[i] + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
t.x, t.y = posX1[i] + (sizeX/2), posY1 + (sizeY/2);
t2[i] = t.value
--t1 = t1..t.value
-- elseif (((t.x >= ((sizeX/2) + posX2 - ((2/3) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((1/3) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((2/3) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((1/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX2 - ((1/3) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((2/3) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((1/3) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((2/3) * sizeY)))) or ((t.x >= ((sizeX/2) + posX2 - ((1/2) * sizeX))) and (t.y >= ((sizeY/2) + posY2 - ((1/2) * sizeY))) and (t.x <= ((sizeX/2) + posX2 + ((1/2) * sizeX))) and (t.y <= ((sizeY/2) + posY2 + ((1/2) * sizeY))))) then
-- t.x, t.y = posX2 + (sizeX/2), posY2 + (sizeY/2);
end
end
t1 = t1..t.value
t.xScale = 1
t.yScale = 1
--print(tile.label)
display.getCurrentStage():setFocus( nil )
t.isFocus = false
--n = n + 1
--value = " "
end
end
这是我的代码,因为它我也有一个值附加到每个瓷砖,例如瓷砖a =“A”,目前我只是在每次移动瓷砖时附加一个字符串,但我想要字符串或也可以使用一个表,只有在将瓷砖放入容器时才附加。例如,如果一个字母位于错误的位置并且我将它移动到正确的位置,我已经触摸了两次,所以我的最终字符串将是错误的
答案 0 :(得分:1)
为什么不在容器上设置一个值:
myContainer.isOccupied = false
然后在您的事件处理程序中,当您进入结束阶段时,您只需进行检查:
if not myContainer.isOccupied then
-- code to drop
myContainer.isOccupied = true
end