我不知道 Corona,但你正在做的事情的一般逻辑是这样的:
- 添加一个事件处理程序,允许您检测何时单击框。
- 添加一些跟踪所选框的方法。
- 单击框时:
- 如果尚未选择框,则选择当前框
- 如果之前选择了另一个框,则与当前框交换
- 如果单击了已选中的框,则忽略(或关闭选中状态)
一般想法(不确定这是否是有效的 Corona 事件处理,但应该让你接近):
box = {}
m={}
m.random = math.random
-- track the currently selected box
local selected = nil
function box:new(x,y)
box.on=false
local box = display.newRect(0,0,100,100)
box:setFillColor(m.random(120,200),m.random(120,200),m.random(120,200))
box.x = x
box.y = y
box.type = "box"
function box:touch(event)
if not selected then
-- nothing is selected yet; select this box
selected = self
-- TODO: change this box in some way to visually indicate that it's selected
elseif selected == self then
-- we were clicked on a second time; we should probably clear the selection
selected = nil
-- TODO: remove visual indication of selection
else
-- swap positions with the previous selected box, then clear the selection
self.x, self.y, selected.x, selected.y
= selected.x, selected.y, self.x, self.y
selected = nil
end
end
return box
end