电晕中的Lua Tic Tac Toe教程

时间:2016-11-04 16:06:06

标签: lua corona

我正在尝试在Corona SDK中完成这个Lua tic-tac-toe教程。 我设法通过1st part,但在2nd part期间他迷失了,他在表格中建立变量以记录" x"和" o"。

他正在使用点按数来确定它是哪个转弯,我确实尝试使用Corona的touch.id来模仿这种技术,但无济于事。

我希望有人可以解释如何使用Corona实现这一目标。

这是我到目前为止(来自教程的第1部分):

d = display
w20 = d.contentWidth * .2
h20 = d.contentHeight * .2 
w40 = d.contentWidth * .4
h40 = d.contentHeight * .4
w60 = d.contentWidth * .6
h60 = d.contentHeight * .6
w80 = d.contentWidth * .8
h80 = d.contentHeight * .8


----DRAW LINES FOR BOARD
local lline = d.newLine(w40,h20,w40,h80 )
lline.strokeWidth = 5

local rline = d.newLine(w60,h20,w60,h80 )
rline.strokeWidth = 5

local bline = d.newLine(w20,h40,w80,h40 )
bline.strokeWidth = 5

local tline = d.newLine(w20,h60,w80,h60 )
tline.strokeWidth = 5


--PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE
board ={

    {"tl", 1, w20, h40, w40, h20,0},
    {"tm",2, w40,h40,w60,h20,0},
    {"tr",3, w60,h40,w80,h20,0},

    {"ml", 4, w20, h60, w40, h40,0},
    {"mm",5, w40,h60,w60,h40,0},
    {"mr",6, w60,h60,w80,h40,0},

    {"bl", 7, w20, h80, w40, h60,0},
    {"bm",8, w40,h80,w60,h60,0},
    {"br",9, w60,h80,w80,h60,0}
  }
--

--FILL COMPARTMENT W/ COLOR WHEN TOUCHED
local function fill (event)
  if event.phase == "began" then
    tap =  0

    for t = 1, 9 do
      if event.x > board[t][3] and event.x < board [t][5] then
        if event.y < board[t][4] and event.y > board[t][6] then

          r = d.newRect(board[t][3],board [t][6],w20,h20)
          r:setFillColor(1,1,0)
          r.anchorX=0
          r.anchorY=0
        end
      end
    end 
  end

end
Runtime:addEventListener("touch", fill)

1 个答案:

答案 0 :(得分:0)

我使用新变量whichTurn来确定每个回合中要放入的内容。我认为代码是不言自明的。尝试

...

local EMPTY, X, O = 0, 1, 2
local whichTurn = X -- X is starting game

... 

--FILL COMPARTMENT W/ COLOR WHEN TOUCHED
local function fill (event)
  if event.phase == "began" then
    for t = 1, 9 do
      if event.x > board[t][3] and event.x < board [t][5] then
        if event.y < board[t][4] and event.y > board[t][6] then
          if board[t][7] == EMPTY then
             board[t][7] = whichTurn
             --[[
             The operator AND returns its first argument if it is false;
             otherwise, it returns its second argument. The operator OR
             returns its first argument if it is not false; otherwise, it
             returns its second argument. 
             --]]
             whichTurn = whichTurn == X and O or X
          end
        end
      end
    end 
  end

end
Runtime:addEventListener("touch", fill)

您可以在Corona中详细了解点击和触摸事件之间的差异。

另请参阅Corona documentation

  

过滤多个分机

     

使用event.numTaps属性,您可以轻松确定是否   对象被多次轻击并同时忽略单击   在对象上。