码迷,mamicode.com
首页 > 其他好文 > 详细

用quick制作仿PkrCruncher

时间:2015-01-08 22:52:36      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:quick-cocos2d-x   源码   

将自己写的一段代码贴上来,抽取的是业务逻辑层,结构比较完整,我将太具体的东西都删除掉了。里面有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 一下)

用quick制作仿PkrCruncher

标签:quick-cocos2d-x   源码   

原文地址:http://blog.csdn.net/helius_sun/article/details/42529503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!