0

我是新来编程这个问题可能听起来很简单。我创建了一个对象作为一个名为 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 中,我想创建尽可能多的盒子,就像冒险游戏一样,我如何切换两个盒子的位置,例如我点击其中一个,然后它被选中,然后我只需点击另一个他们互相改变位置。提前致谢

4

1 回答 1

1

我不知道 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
于 2012-09-12T17:57:04.157 回答