0

私はプログラミングに慣れていないので、この質問は非常に単純に聞こえるかもしれません。ボックスというモジュールとしてオブジェクトを作成しました

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で、できるだけ多くのボックスを作成したいのですが、アドベンチャーゲームのように、ボックスの2つの位置を切り替えるにはどうすればよいですか?たとえば、そのうちの1つをクリックすると、それが選択され、もう1つをクリックして彼らはお互いに位置を変えます。前もって感謝します

4

1 に答える 1

1

コロナについてはわかりませんが、あなたがしていることの一般的な論理は次のとおりです。

  • ボックスがクリックされたことを検出できるイベント ハンドラーを追加します。
  • 選択したボックスを追跡する何らかの方法を追加します。
  • ボックスをクリックすると:
    • ボックスがまだ選択されていない場合は、現在のボックスを選択します
    • 以前に別のボックスが選択されていた場合は、現在のボックスと入れ替えます
    • すでに選択されているボックスがクリックされた場合は、無視します (または選択をオフに切り替えます)

一般的な考え方 (これが有効なコロナ イベント処理かどうかはわかりませんが、近づく必要があります):

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 に答える