學習quick cocos2d-x 次日 ,使用quick-x 作了一個井字棋遊戲 。php
我假設讀者已經 http://wiki.quick-x.com/doku.php?id=zh_cn閱讀了這個連接下的內容 ,並學會了如何搭建環境和建立新的工程,並假高讀者有必定cocos2d-x基礎 android
建議讀者多研究一下quick-x自帶的例子coinflip。並閱讀framework下的lua源碼,尤爲注意用lua模擬出面象對象的部分(可參考《Lua程序設計》第二版的13,16兩章)。學習
一。準備工做:ui
1.如何在場景(層)添加一個Spritelua
咱們在MainScene中添加一個Sprite spa
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)
endcode
display 是處理顯示有關的一個「類」。對象
newSprite則相似cocos2d-x中的CCSprite::create()blog
注意: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事件
3.1在 Board:actor中增長如下代碼
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
搞了一天,有點累了,寫的不詳細,有問題請你們在評論裏問吧