將自己寫的一段代碼貼上來,抽取的是業務邏輯層,結構比較完整,我將太具體的東西都刪除掉了。裏面有tableview,幀動畫,替換精靈圖片,獲取系統時間。
local CalWinRateScene = class("CalWinRateScene", function()
return display.newScene("CalWinRateScene")
end)
function CalWinRateScene:ctor()
self.layer = display.newLayer()
self:addChild(self.layer)
-- 生成數據
self:createData()
-- 添加內容和標題
self:addContent()
self:addTopTitle()
self:addTable()
-- 默認選中狀態爲A
self.layer:getChildByTag(44):getChildByTag(1111):getChildByTag(22):setEnabled(false)
-- 默認焦點在6號位置
self.layer:getChildByTag(1000):cellAtIndex(0):getChildByTag(1):setVisible(true)
end
function CalWinRateScene:addTopTitle()
require("app.Utils.GeneralSceneFactory").new({parentNode = self.layer, title="概率計算器",
backFunction = function()
end})
end
function CalWinRateScene:runFanPaiAnimation(node, calBackFunc, others)
CCDirector:sharedDirector():getActionManager():removeAllActionsFromTarget(node)
-- 進入的時候加載幀動畫
local sharedSpriteFrameCache = CCSpriteFrameCache:sharedSpriteFrameCache()
local animation = display.newAnimation({ sharedSpriteFrameCache:spriteFrameByName(others.beginCard),
sharedSpriteFrameCache:spriteFrameByName("animation1.png"),
sharedSpriteFrameCache:spriteFrameByName("animation2.png"),
sharedSpriteFrameCache:spriteFrameByName("animation3.png"),
sharedSpriteFrameCache:spriteFrameByName("animation4.png"),
sharedSpriteFrameCache:spriteFrameByName("animation5.png"),
sharedSpriteFrameCache:spriteFrameByName(others.endCard)},
0.04)
local animate = CCAnimate:create(animation)
local func = CCCallFuncN:create(calBackFunc)
local array = CCArray:create()
array:addObject(func)
array:addObject(animate)
node:runAction(CCSequence:create(array))
end
function CalWinRateScene:runGaiPaiAnimation(node, callBackFunc, others)
end
-- 設置選中之後圖標和提示文字的顏色
function CalWinRateScene:setColorSelect(index)
local sharedSpriteFrameCache = CCSpriteFrameCache:sharedSpriteFrameCache()
local frame = sharedSpriteFrameCache:spriteFrameByName("3hua0" .. 1+index .. ".png")
self.layer:getChildByTag(44):getChildByTag(101+index):setColor(ccc3(163, 163, 163))
self.layer:getChildByTag(44):getChildByTag(105+index):setDisplayFrame(frame)
end
-- 還原選中之後的圖標和提示文字的顏色
function CalWinRateScene:setColorUnselect(index)
end
-- 判斷是否應該,還原相應的花色牌
function CalWinRateScene:needRebackColor(info, term)
local preString = string.sub(info, 1, 1)
local intPrevString = self:stringToInt(info)
local nextString = string.sub(term, 1, 1)
if preString == nextString then
local rebackIndex = math.ceil(intPrevString/13)
self.layer:getChildByTag(44):getChildByTag(1111):getChildByTag(rebackIndex+27):setEnabled(true)
self:setColorUnselect(rebackIndex-1)
end
end
-- 將選中點得選中狀態還原
function CalWinRateScene:resetSelectedState(index)
if index < 6 then
-- 公共牌
self.layer:getChildByTag(44):getChildByTag(1111):getChildByTag(index+32):setEnabled(true)
else
-- 選手牌
local tempTable = self:getCellIndexAndSpriteIndex(index)
local cellIndex = tempTable.cellIndex+1
local spriteIndex = tempTable.spriteIndex
local cell = self.layer:getChildByTag(1000):cellAtIndex(cellIndex-1)
if cell then
cell:getChildByTag(spriteIndex):setVisible(false)
end
end
end
-- 添加選中點得狀態
function CalWinRateScene:addSelectedState(index)
end
-- 當焦點在tableView中換行時,要做offset的偏移
function CalWinRateScene:makeTableViewMove()
end
-- 獲得tableview的列和精靈的相應索引
function CalWinRateScene:getCellIndexAndSpriteIndex(index)
local cellIndex = math.ceil((index-5)/4)
local spriteIndex = cellIndex*4 - (index-5)
if spriteIndex == 0 then
spriteIndex = 4
else
spriteIndex = 4-spriteIndex
end
local tempTable = {}
tempTable.cellIndex = cellIndex-1
tempTable.spriteIndex = spriteIndex
return tempTable
end
function CalWinRateScene:addContent()
local function selectPocker(text, tag)
end
local function selectPublicCard(tag)
end
local function selectDetailCard(tag)
end
local function resetAll()
end
-- 撤銷操作
local function cancelSelect()
end
local function addMenuItemClick(tag)
if tag == 11 then
resetAll()
elseif tag == 12 then
-- 刪除按鈕
elseif tag == 13 then
-- 增加按鈕
elseif tag == 14 then
local info = {}
info.HoleCard1 = self.playerCards[1][1]
info.HoleCard2 = self.playerCards[1][2]
info.PublicCards = self.publicCards
info.publicCardsCount = 5
info.PlayerCards = {}
for i=2,self.playerNum do
for j=1,2 do
info.PlayerCards[#info.PlayerCards+1] = self.playerCards[i][j]
end
end
info.PlayerCount = self.playerNum -1
local limit = os.time()
local date=os.date("%Y-%m-%d %H:%M:%S", limit)
print("the begin date is " .. date)
local res = BookUtils:calWinRate(json.encode(info))
print("the res is " .. res)
res = string.sub(res, 1, #res-1)
local resarray = string.split(res, ";")
local tableView = self.layer:getChildByTag(1000)
table.walk(resarray, function(val, key)
local temp = string.sub(val, 1, 6)
local numTemp = tonumber(temp)
local finalTemp = numTemp*100 .. "%"
self.winRate[tonumber(key)] = finalTemp
end)
local height = tableView:getContentOffset().y
tableView:reloadData()
tableView:setContentOffset(CCPoint(0, height), false)
local limit = os.time()
local date=os.date("%Y-%m-%d %H:%M:%S", limit)
print("the end date is " .. date)
elseif tag == 15 then
-- 選牌:8
selectPocker("8", 15)
elseif tag == 16 then
-- 選牌:7
selectPocker("7", 16)
elseif tag == 17 then
-- 選牌:6
selectPocker("6", 17)
elseif tag == 18 then
-- 選牌:5
selectPocker("5", 18)
elseif tag == 19 then
-- 選牌:4
selectPocker("4", 19)
elseif tag == 20 then
-- 選牌:3
selectPocker("3", 20)
elseif tag == 21 then
-- 選牌:2
selectPocker("2", 21)
elseif tag == 22 then
-- 選牌:A
selectPocker("A", 22)
elseif tag == 23 then
-- 選牌:K
selectPocker("K", 23)
elseif tag == 24 then
-- 選牌:Q
selectPocker("Q", 24)
elseif tag == 25 then
-- 選牌:J
selectPocker("J", 25)
elseif tag == 26 then
-- 選牌:10
selectPocker("10", 26)
elseif tag == 27 then
-- 選牌:9
selectPocker("9", 27)
elseif tag == 28 then
-- 區域:黑桃
selectDetailCard(28)
elseif tag == 29 then
-- 區域:紅心
selectDetailCard(29)
elseif tag == 30 then
-- 區域:草花
selectDetailCard(30)
elseif tag == 31 then
-- 區域:方片
selectDetailCard(31)
elseif tag == 32 then
-- 區域:撤銷
cancelSelect()
elseif tag == 33 then
-- 翻牌1
selectPublicCard(33)
elseif tag == 34 then
-- 翻牌2
selectPublicCard(34)
elseif tag == 35 then
-- 翻牌3
selectPublicCard(35)
elseif tag == 36 then
-- 轉牌
selectPublicCard(36)
elseif tag == 37 then
-- 河牌
selectPublicCard(37)
end
end
local fileName = "app.data.WinRateData"
require("app.Utils.NodeInflater"):easyRootInflater():inflate(fileName, self.layer)
local menuItems = self.layer:getChildByTag(44):getChildByTag(1111):getChildren()
for i=1,menuItems:count() do
menuItems:objectAtIndex(i-1):registerScriptTapHandler(addMenuItemClick)
end
end
function CalWinRateScene:createData()
-- 公共牌
self.publicCards = {"-1", "-1", "-1", "-1", "-1"}
-- 對手牌
self.playerCards = {}
for i=1,10 do
self.playerCards[i] = {"-1", "-1"}
end
-- 玩家的個數
self.playerNum = 2
-- 選擇的牌 2-A
self.selectPoker = "A"
-- 選擇的花色 h,d,s,c
self.selectColor = "-1"
-- 選牌,焦點所在的位置 公共牌1-5, 牌手的牌6-10, 10-14
self.focusPoint = 6
-- 數據
self.winRate = {}
for i=1,10 do
self.winRate[i] = ""
end
-- cell的高度
self.cellHeight = 218
end
-- 將牌型的string轉化成int
function CalWinRateScene:stringToInt(text)
end
function CalWinRateScene:addTable()
local winSize = CCDirector:sharedDirector():getWinSize()
local tableView = CCTableView:create(CCSizeMake(display.width, display.height-696-STATUSBARH))
tableView:setDirection(kCCScrollViewDirectionVertical)
tableView:setVerticalFillOrder(kCCTableViewFillTopDown)
tableView:setPosition(CCPointMake(0, 360))
tableView:setBounceable(true)
tableView:setTag(1000)
self.layer:addChild(tableView)
tableView:setZOrder(1)
tableView:registerScriptHandler(handler(self, self.scrollViewDidScroll),CCTableView.kTableViewScroll)
tableView:registerScriptHandler(handler(self, self.scrollViewDidZoom),CCTableView.kTableViewZoom)
tableView:registerScriptHandler(handler(self, self.tableCellTouched),CCTableView.kTableCellTouched)
tableView:registerScriptHandler(handler(self, self.cellSizeForTable),CCTableView.kTableCellSizeForIndex)
tableView:registerScriptHandler(handler(self, self.tableCellAtIndex),CCTableView.kTableCellSizeAtIndex)
tableView:registerScriptHandler(handler(self, self.numberOfCellsInTableView),CCTableView.kNumberOfCellsInTableView)
tableView:registerScriptHandler(handler(self, self.tableCellHighlight),CCTableView.kTableCellHighLight)
tableView:registerScriptHandler(handler(self, self.tableCellUnhighlight),CCTableView.kTableCellUnhighLight)
tableView:reloadData()
end
-- tableview的代理
function CalWinRateScene:scrollViewDidScroll(view)
end
function CalWinRateScene:scrollViewDidZoom(view)
end
function CalWinRateScene:tableCellTouched(table,cell)
local idx = cell:getIdx()
local num = idx + 1
end
function CalWinRateScene:cellSizeForTable(table,idx)
return self.cellHeight, 640
end
-- 將公共牌和玩家的焦點清空
function CalWinRateScene:clearFocus()
end
function CalWinRateScene:tableCellAtIndex(table, idx)
local num = idx + 1
local cell = table:dequeueCell()
if nil == cell then
cell = CCTableViewCell:new()
else
cell:removeAllChildrenWithCleanup(true)
end
-- index的取值1:左1 2:左2 3:右1 4:右2
local function createCard(posx, posy, index)
end
local function createBg(posx, poxy)
end
local function creatWhiteBg(posx, posy)
end
local function creatLable(posx, posy, index)
end
local function addRight()
createBg(330, 0)
createCard(350, 198, 3)
createCard(470, 198, 4)
creatWhiteBg(380, 9)
creatLable(460, 29, 2)
end
local function addLeft()
createBg(50, 0)
createCard(70, 198, 1)
createCard(190, 198, 2)
creatWhiteBg(100, 9)
creatLable(180, 29, 1)
end
if num < math.ceil(self.playerNum*1.0/2) then
addLeft()
addRight()
elseif num == math.ceil(self.playerNum*1.0/2) then
if self.playerNum/2 == math.ceil(self.playerNum*1.0/2) then
addLeft()
addRight()
else
addLeft()
end
end
return cell
end
function CalWinRateScene:numberOfCellsInTableView(table)
return math.ceil(self.playerNum/2)
end
function CalWinRateScene:tableCellHighlight(table, cell)
end
function CalWinRateScene:tableCellUnhighlight(table, cell)
end
return CalWinRateScene
最終的效果圖如下:
csdn不能傳視頻,所以只傳一張靜態圖。具體效果可以下載app:德州培訓大師體驗。(ps:美術大哥發了一個壓縮圖片的網址:點擊打開鏈接,mark 一下)