我假设读者已经http://wiki.quick-x.com/doku.PHP?id=zh_cn阅读了这个链接下的内容 ,并学会了如何搭建环境和创建新的工程,并假高读者有一定cocos2d-x基础
建议读者多研究一下quick-x自带的例子coinflip。并阅读framework下的lua源码,尤其注意用lua模拟出面象对象的部分(可参考《Lua程序设计》第二版的13,16两章)。
一。准备工作:
1.如何在场景(层)添加一个Sprite
我们在MainScene中添加一个Sprite
function MainScene:ctor()
self.bg = display.newSprite("board.png",display.cx,display.cy)
self:addChild(self.bg)
-- keypad layer,for android
self.layer = Board.new()
self:addChild(self.layer)
end
display 是处理显示有关的一个“类”。
newSprite则类似cocos2d-x中的CCSprite::create()
注意:Lua中除用local 修饰的变量都是全局变量。我们self.bg这样定义,而不直接定义,目的是不污染全局环境,和把bg作为MainScene“类”(其实是表)的一个变量。
2.定义一个Layer
Board是我定义的一个层,添加在MainScene上。
定义层的方法为:
在Board.lua文件 中
local Board = class("Board",function() return display.newLayer() end)
return Board
大家可以到framework下看看class是如何实现的。
3.如何增加touch事件
self:addTouchEventListener(handler(self,self.onTouch)) self:setNodeEventEnabled(true)
3.2 在onEnter,onExit中分别设置和移除相关事件监听
function Board:onEnter() self:setTouchEnabled(true) end function Board:onExit() self:removeAllEventListeners() end
3.3 在Board:onTouch中处理事件
function Board:onTouch(event,x,y) //TODO 处理点击事件 end
二。定义数据
我使用一个2维的表来描述整个棋盘(也可以使用一维表)
myBoard = {{"-","-","-"},{"-","-"}} theWiner = "-1"
myBoard即棋盘,“-1”表示没有棋子,“X”表示有“X”形棋子,“O”表示有“O”型棋子。
theWiner表示获胜者,初始为-1。
三。程序流程
1.玩家点击事件后,在相应的位置放置棋子,并修改myBoard数据
比如简单,直接附代码了,写的比较粗糙,因为 也刚学Lua才两三天。
turn = "O" function Board:makeMove(x,y) if theWiner ~= "-1" then return end row,co = self:getBoardLocation(x,y) if row == -1 then return end self:makeEle(row,co) end function Board:makeEle(row,co) local file = "piece_o.png" if turn == "X" then file = "piece_x.png" else file = "piece_o.png" end myBoard[row][co] = turn; self.ele = display.newSprite(file,display.cx+100*(co-2),display.cy+100*(2-row)) self:addChild(self.ele) local ret = Board:winCheck(row,co) print("winCheck",ret) if ret == "O" then self.lable:setString("O is the winer") end if ret == "X" then self.lable:setString("X is the winner") end if ret == "He" then self.lable:setString("No one is the winner") end if ret == "Wh" then self.lable:setString("Continue") end if turn == "X" then turn = "O" else turn = "X" end end
function Board:getBoardLocation(x,y) if x < display.cx-150 or x >display.cx+150 then return -1 end if y > display.cy+150 or y < display.cy-150 then return -1 end local co if x <= display.cx - 50 then co = 1 elseif x > display.cx-50 and x < display.cx+50 then co = 2 else co = 3 end local row if y <= display.cy - 50 then row = 3 elseif y > display.cy-50 and y < display.cy+50 then row = 2 else row = 1 end return row,co end
2.检查玩家是否获胜或平局
function Board:winCheck(row,co) local cur = myBoard[row][co] if myBoard[1][2] == cur and myBoard[1][3] == cur and myBoard[1][1] ==cur then return cur end if myBoard[2][2] == cur and myBoard[2][3] == cur and myBoard[2][1] == cur then return cur end if myBoard[3][2] == cur and myBoard[3][3] == cur and myBoard[3][1] == cur then return cur end if myBoard[1][1] == cur and myBoard[2][1] == cur and myBoard[3][1] == cur then return cur end if myBoard[1][2] == cur and myBoard[2][2] == cur and myBoard[3][2] == cur then return cur end if myBoard[1][3] == cur and myBoard[2][3] == cur and myBoard[3][3] == cur then return cur end if myBoard[1][1] == cur and myBoard[2][2] == cur and myBoard[3][3] == cur then return cur end if myBoard[1][3] == cur and myBoard[2][2] == cur and myBoard[3][1] == cur then return cur end open = true; for i = 1,3 do for j = 1,3 do if myBoard[i][j] == "-" then open = false end end end if open then return "He" else return "Wh" end end
搞了一天,有点累了,写的不详细,有问题请大家在评论里问吧