我是编程这个问题的新手可能听起来很简单。 我创建了一个对象作为名为box
的模块box = {}
m={}
m.random = math.random
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"
return box
end
return box
在我的main.lua中我想创建尽可能多的盒子,就像冒险游戏一样,如何切换两个盒子的位置,例如我点击其中一个,然后选中它,我只需点击另一个,他们互相改变立场。 提前致谢
答案 0 :(得分:1)
我不知道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