采用cocos2d-x lua 制作数字滚动效果样例

require "Cocos2d"
require "Cocos2dConstants"
local testscene = class("testscene",function()
        return cc.Scene:create()
end)
function testscene:create()
local  scene = testscene.new()
    return scene
end
--默认函数
function testscene:ctor()
--创建层-将层加入场景中
    local layer = cc.Layer:create()
    self:addChild(layer)
--创建节点-将节点加入层中
    local node = cc.Sprite:create("res/land.png")
    node:setPosition(240,160)
    node:setAnchorPoint(0.5,0.5)
    layer:addChild(node,2)

    local size = node:getContentSize()
        _beginNum = 200
--创建文本-将文本加入节点中
    txt =  cc.LabelTTF:create(tostring(_beginNum),"Marker Felt",24)
        txt:setColor(cc.c4b(20,25,255,255))
        txt:setPosition(size.width/2,size.height/2)
        txt:setAnchorPoint(0.5,0.5)
        node:addChild(txt)
    local taction = {}
--延迟
    local delay =cc.DelayTime:create(0.01)
        table.insert(taction,delay)
--扩大
        local scale = cc.ScaleTo:create(0.2,2)
        local scale2 = cc.ScaleTo:create(0.2,1)
           table.insert(taction,scale)
           table.insert(taction,scale2)
-- 数值改变
        _endNum =10000
        math.randomseed(3000)
        _dis = math.random(5000,10000)
        print(_dis)
    local rtime = (_endNum-_beginNum)/_dis
        print(rtime)
    function chagenum()
        if (_beginNum <  _endNum) then
           _beginNum= _beginNum +_dis
            txt:setString(_beginNum)
        elseif (_beginNum ==  _endNum) then
            txt:setString(  _endNum)
        end
    end
    local seq = cc.Sequence:create(delay,cc.CallFunc:create(chagenum))
--数字翻滚
    local X = size.width/2
    local Y = size.height/2
    local UPY = 2*Y
    local DownY = 0
    local move1 = cc.MoveTo:create(0.05,cc.p(X,UPY))
    local move2 = cc.MoveTo:create(0.05,cc.p(X,DownY))
    local move3 = cc.MoveTo:create(0.05,cc.p(X,Y))

    local scale1 = cc.ScaleTo:create(0.05,1,0.001)
    local scale2 = cc.ScaleTo:create(0.05,1,1)

    local UP =   cc.Sequence:create(move1,scale1)
    local Down =   cc.Sequence:create(move2,scale1,scale2,move3)
    local spawn = cc.Spawn:create(UP,seq,Down)
    local rep = cc.Repeat:create(spawn,rtime)
--设置真值
    local function setnum()
        _beginNum =  _endNum
        txt:setString(_beginNum)
    end

    local call = cc.CallFunc:create(setnum)
    local seq2 = cc.Sequence:create(rep,delay,call)
        table.insert(taction,seq2)
        table.insert(taction,scale)
        table.insert(taction,scale2)
    local seqaction = cc.Sequence:create(taction)
    txt:runAction(seqaction)

    return layer
end

return testscene

 

转载于:https://www.cnblogs.com/annapolis/p/4789397.html

你可能感兴趣的:(采用cocos2d-x lua 制作数字滚动效果样例)