cocos2d-x TestLua 之 CCEase 变速类

感谢点评与关注,欢迎转载与分享。
勤奋努力,持之以恒!

变速类行为

这类行为的作用就是改变另一行为的速度,基类是CCEaseAction,继承自CCActionInterval。

CCEaseIn  由

CCEaseOut  由

CCEaseInOut  由快至慢再由慢至快

CCEaseSineIn  由快至慢

CCEaseSineOut  由慢至快

CCEaseSineInOut  由快至慢再由慢至快

CCEaseExponentianIn  由极快至慢

CCEaseExponentianOut  由慢至极快

CCEaseExponentianInOut  由极快至慢再由慢至极快

CCActionmanager

这个类其实并不属于行为类,它的父类是NSObject,而不是CCAction,但它又与CCAction密不可分,因此在这里单独介绍下。

CCActionmanager是个标准的单例类,它的作用顾名思义,就是管理行为类的对象,工作原理是:当节点执行runAction时,会把action通过addAction方法将对象传递给CCActionmanager的单例,该实例再把这个action添加到自己的行为序列中。CCActionmanager通过schedule定时刷新自己的update方法,在这个方法中去调用行为序列中每个action的step(会有一些筛选条件,比如暂停的行为不会update),这些step方法再根据自身的完成进度去update或是结束行为。就是说实际上是由CCActionmanager驱动的每个action去更新自己的逻辑,而runAction方法只是将行为对象添加进CCActionmanager的更新队列罢了。当节点被清除或是行为结束时,CCActionmanager会自动将action从队列中剔除,无需开发者操心。


local size = CCDirector:sharedDirector():getWinSize()

local scheduler = CCDirector:sharedDirector():getScheduler()


local sp1 = nil
local sp2 = nil
local sp3 = nil

local function createSimpleMoveBy()
    return CCMoveBy:create(3,ccp(size.width-130,0))
end

local function createSimpleDelayTime()
    return CCDelayTime:create(0.25)
end

local function getBaseLayer()
    local layer = CCLayer:create()
    
    sp1 = CCSprite:create("man.png")
    sp2 = CCSprite:create("blackgirl.png")
    sp3 = CCSprite:create("whitegirl.png")
    
    layer:addChild(sp1,3)
    layer:addChild(sp2,2)
    layer:addChild(sp3,1)
    
    sp1:setPosition(ccp(60,size.height*1/5))
    sp2:setPosition(ccp(60,size.height*2.5/5))
    sp3:setPosition(ccp(60,size.height*4/5))

    return layer
end

local logic = nil

local function update()
    scheduler:unscheduleScriptEntry(logic)
    sp1:stopActionByTag(11)
    sp2:stopActionByTag(22)
    sp3:stopActionByTag(33)
end

local function enterOrExitCallFunc(sender)
    if sender == "enter" then
        logic = scheduler:scheduleScriptFunc(update,6.5,false)
    elseif sender == "exit" then
        scheduler:unscheduleScriptEntry(logic)
    end
end

local function SpriteEase()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()
    local move_back = move:reverse()
    
    local move_ease_in = CCEaseIn:create(createSimpleMoveBy(),2.5) --由慢至快(速度线性变化),慢——>快
    local move_ease_in_back = move_ease_in:reverse()
    
    local move_ease_out = CCEaseOut:create(createSimpleMoveBy(),2.5) --由快至慢,快——>慢

    local move_ease_out_back = move_ease_out:reverse()
    
    local arr1 = CCArray:create()
    arr1:addObject(move)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_back)
    arr1:addObject(createSimpleDelayTime())
    local seq1 = CCSequence:create(arr1)
    seq1:setTag(11)
    
    local arr2 = CCArray:create()
    arr2:addObject(move_ease_in)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_in_back)
    arr2:addObject(createSimpleDelayTime())
    local seq2 = CCSequence:create(arr2)
    seq2:setTag(22)
    
    local arr3 = CCArray:create()
    arr3:addObject(move_ease_out)
    arr3:addObject(createSimpleDelayTime())
    arr3:addObject(move_ease_out_back)
    arr3:addObject(createSimpleDelayTime())
    local seq3 = CCSequence:create(arr3)
    seq3:setTag(33)
    
    sp1:runAction(seq1)
    sp2:runAction(seq2)
    sp3:runAction(seq3)
    
    layer:registerScriptHandler(enterOrExitCallFunc)
    
    return layer
end
local scene = CCScene:create()
scene:addChild(SpriteEase())
CCDirector:sharedDirector():runWithScene(scene)
local function SpriteEaseInOut()
    local layer = getBaseLayer()
    
    local move_ease_inout1 = CCEaseInOut:create(createSimpleMoveBy(),5) --第2个参数>1,则由慢至快再由快至慢,慢快慢
    local move_ease_inout1_back = move_ease_inout1:reverse() 
    
    local move_ease_inout2 = CCEaseInOut:create(createSimpleMoveBy(),1) --第2个参数=1,则匀速运动
    local move_ease_inout2_back = move_ease_inout2:reverse()
    
    local move_ease_inout3 = CCEaseInOut:create(createSimpleMoveBy(),0.3) --第2个参数<1,则由快至慢再由慢至快,快慢快
    local move_ease_inout3_back = move_ease_inout3:reverse()
    
    local arr1 = CCArray:create()
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_ease_inout1)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_ease_inout1_back)
    
    local arr2 = CCArray:create()
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_inout2)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_inout2_back)
    
    local arr3 = CCArray:create()
    arr3:addObject(createSimpleDelayTime())
    arr3:addObject(move_ease_inout3)
    arr3:addObject(createSimpleDelayTime())
    arr3:addObject(move_ease_inout3_back)
    
    sp1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
    sp2:runAction(CCRepeatForever:create(CCSequence:create(arr2)))
    sp3:runAction(CCRepeatForever:create(CCSequence:create(arr3)))
    
    return layer
end

local function SpriteEaseExponential()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()
    local move_back = move:reverse()
    
    local arr1 = CCArray:create()
    arr1:addObject(move)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_back)
    sp1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
    
    local move_ease_in = CCEaseExponentialIn:create(createSimpleMoveBy()) --由慢至极快(速度指数级变化)
    local move_ease_in_back = move_ease_in:reverse()
    
    local arr2 = CCArray:create()
    arr2:addObject(move_ease_in)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_in_back)
    sp2:runAction(CCRepeatForever:create(CCSequence:create(arr2)))
    
    local move_ease_out = CCEaseExponentialOut:create(createSimpleMoveBy()) --与CCEaseExponentialIn相反
    local move_ease_out_back = move_ease_out:reverse()
    
    local arr3 = CCArray:create()
    arr3:addObject(move_ease_out)
    arr3:addObject(createSimpleDelayTime())
    arr3:addObject(move_ease_out_back)
    sp3:runAction(CCRepeatForever:create(CCSequence:create(arr3)))
    
    return layer
end

local function SpriteEaseExponentialInOut()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()
    local move_back = move:reverse()

    local arr1 = CCArray:create()
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_back)
    sp1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))

    local move_ease_in_out = CCEaseExponentialInOut:create(createSimpleMoveBy()) --由慢至极快再由极快至慢 慢快慢
    local move_ease_in_out_back = move_ease_in_out:reverse()
    
    local arr2 = CCArray:create()
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_in_out)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_in_out_back)
    sp2:runAction(CCRepeatForever:create(CCSequence:create(arr2)))
    
    return layer
end

local function SpriteEaseSine()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()
    local move_back = move:reverse()
    
    local arr1 = CCArray:create()
    arr1:addObject(move)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_back)
    sp1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
    
    local move_ease_in = CCEaseSineIn:create(createSimpleMoveBy())
    local move_ease_in_back = move_ease_in:reverse()
    
    local arr2 = CCArray:create()
    arr2:addObject(move_ease_in)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_in_back)
    sp2:runAction(CCRepeatForever:create(CCSequence:create(arr2)))
    
    local move_ease_out = CCEaseSineOut:create(createSimpleMoveBy())
    local move_ease_out_back = move_ease_out:reverse()
    
    local arr3 = CCArray:create()
    arr3:addObject(move_ease_out)
    arr3:addObject(createSimpleDelayTime())
    arr3:addObject(move_ease_out_back)
    sp3:runAction(CCRepeatForever:create(CCSequence:create(arr3)))
    
    return layer
end


local function SpriteEaseSineInOut()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()
    local move_back = move:reverse()
    
    local arr1 = CCArray:create()
    arr1:addObject(move)
    arr1:addObject(createSimpleDelayTime())
    arr1:addObject(move_back)
    sp1:runAction(CCRepeatForever:create(CCSequence:create(arr1)))
    
    local move_ease_sine_in_out = CCEaseSineInOut:create(createSimpleMoveBy())
    local move_ease_sine_in_out_back = move_ease_sine_in_out:reverse()
    
    local arr2 = CCArray:create()
    arr2:addObject(move_ease_sine_in_out)
    arr2:addObject(createSimpleDelayTime())
    arr2:addObject(move_ease_sine_in_out_back)
    sp2:runAction(CCRepeatForever:create(CCSequence:create(arr2)))

    return layer
end

--实验知:CCEase...等等一系列里面可以是 CCSequence
local function test()
    local layer = getBaseLayer()
    
    local move = createSimpleMoveBy()

    local rotation = CCRotateBy:create(3,360)

    local jump = CCJumpBy:create(3,ccp(500,0),50,10)
    
    local arr = CCArray:create()
    arr:addObject(move)
    arr:addObject(rotation)
    arr:addObject(jump)
    
    sp1:runAction(CCEaseExponentialIn:create(CCSequence:create(arr)))
    
    return layer
end



你可能感兴趣的:(cocos2d-x TestLua 之 CCEase 变速类)