コロナについてはわかりませんが、あなたがしていることの一般的な論理は次のとおりです。
- ボックスがクリックされたことを検出できるイベント ハンドラーを追加します。
- 選択したボックスを追跡する何らかの方法を追加します。
- ボックスをクリックすると:
- ボックスがまだ選択されていない場合は、現在のボックスを選択します
- 以前に別のボックスが選択されていた場合は、現在のボックスと入れ替えます
- すでに選択されているボックスがクリックされた場合は、無視します (または選択をオフに切り替えます)
一般的な考え方 (これが有効なコロナ イベント処理かどうかはわかりませんが、近づく必要があります):
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