Cocos2d-x 3 1 1 Lua示例 ActionEaseTest(動作)
阿新 • • 發佈:2018-12-21
cc.p(60, s.height * 4 / 5)) -- 初始化層 Helper.initWithLayer(layer) return layerend------------------------------------- SpriteEase-----------------------------------local SpriteEase_entry = nil-- 測試停止動作local function testStopAction(dt) -- 取消排程指令碼 scheduler:unscheduleScriptEntry(SpriteEase_entry) tamara:stopActionByTag(1) kathia:stopActionByTag(1) grossini:stopActionByTag(1)end-- 進入或退出local function SpriteEase_onEnterOrExit(tag) -- 如果是進入 if tag == "enter" then -- 執行指令碼方法 SpriteEase_entry = scheduler:scheduleScriptFunc(testStopAction, 6.25, false) elseif tag == "exit" then -- 登出迴圈 scheduler:unscheduleScriptEntry(SpriteEase_entry) endend--local function SpriteEase() local layer = getBaseLayer() local move = createSimpleMoveBy() local move_back = move:reverse() -- 原地返回 -- 動作由慢變快 local move_ease_in = cc.EaseIn:create(createSimpleMoveBy(), 2.5) -- 原路返回 local move_ease_in_back = move_ease_in:reverse() -- 動作有塊變慢 local move_ease_out = cc.EaseOut:create(createSimpleMoveBy(), 2.5) -- 原路返回 local move_ease_out_back = move_ease_out:reverse() -- 延時 local delay = createSimpleDelayTime() -- 動作序列1 local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) -- 動作序列2 local seq2 = cc.Sequence:create(move_ease_in,createSimpleDelayTime(),move_ease_in_back,createSimpleDelayTime()) -- 動作序列3 local seq3 = cc.Sequence:create(move_ease_out,createSimpleDelayTime(),move_ease_out_back,createSimpleDelayTime()) -- 執行一個無線迴圈的動作 local a2 = grossini:runAction(cc.RepeatForever:create(seq1)) a2:setTag(1)--改變這個用來更容易分辨節點的標記。 local a1 = tamara:runAction(cc.RepeatForever:create(seq2)) a1:setTag(1) local a = kathia:runAction(cc.RepeatForever:create(seq3)) a:setTag(1) -- 註冊監聽指令碼 layer:registerScriptHandler(SpriteEase_onEnterOrExit) Helper.titleLabel:setString("EaseIn - EaseOut - Stop") return layerend------------------------------------- SpriteEaseInOut 精靈由慢至快再由快至慢-----------------------------------local function SpriteEaseInOut() -- 獲取基礎層 local layer = getBaseLayer() -- 建立移動動作 local move = createSimpleMoveBy() -- 建立由慢變快再由快變慢的動作,第一個引數表示動作,第二個引數表示頻率 local move_ease_inout1 = cc.EaseInOut:create(createSimpleMoveBy(), 0.65) -- 返回執行與本Action物件相反操作的新Action物件 local move_ease_inout_back1 = move_ease_inout1:reverse() -- 建立頻率為1.35的easeInOut動作 local move_ease_inout2 = cc.EaseInOut:create(createSimpleMoveBy(), 1.35) -- 返回執行與本Action物件相反操作的新Action物件 local move_ease_inout_back2 = move_ease_inout2:reverse() -- 建立頻率為1.0的easeInOut動作 local move_ease_inout3 = cc.EaseInOut:create(createSimpleMoveBy(), 1.0) -- 返回執行與本Action物件相反操作的新Action物件 local move_ease_inout_back3 = move_ease_inout3:reverse() -- 延時動作 local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move_ease_inout1,delay,move_ease_inout_back1,createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease_inout2,createSimpleDelayTime(),move_ease_inout_back2,createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime() ) tamara:runAction(cc.RepeatForever:create(seq1)) kathia:runAction(cc.RepeatForever:create(seq2)) grossini:runAction(cc.RepeatForever:create(seq3)) Helper.titleLabel:setString("EaseInOut and rates") return layerend------------------------------------- SpriteEaseExponential 精靈由慢至極快,由極快到慢-----------------------------------local function SpriteEaseExponential() -- 得到基礎層 local layer = getBaseLayer() -- 建立移動動作 local move = createSimpleMoveBy() -- 反操作 local move_back = move:reverse() -- 建立由慢到極快的動作 local move_ease_in = cc.EaseExponentialIn:create(createSimpleMoveBy()) local move_ease_in_back = move_ease_in:reverse() -- 建立有極快到慢的動作 local move_ease_out = cc.EaseExponentialOut:create(createSimpleMoveBy()) local move_ease_out_back = move_ease_out:reverse() -- 建立延時動作 local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) -- 三個醜八怪執行動作 grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) kathia:runAction(cc.RepeatForever:create(seq3)) -- 設定層標題 Helper.titleLabel:setString("ExpIn - ExpOut actions") return layerend------------------------------------- SpriteEaseExponentialInOut 精靈由慢至極快再由極快至慢-----------------------------------local function SpriteEaseExponentialInOut() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() -- 反向操作 local move_back = move:reverse() -- 建立由慢至極快再由極快至慢的動作 local move_ease = cc.EaseExponentialInOut:create(createSimpleMoveBy()) local move_ease_back = move_ease:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime() ) -- 設定兩個醜八怪的位置 positionForTwo() grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) -- 設定層標題 Helper.titleLabel:setString("EaseExponentialInOut action") return layerend------------------------------------- SpriteEaseSine 由快至慢、由慢至快-----------------------------------local function SpriteEaseSine() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() -- 反向操作 local move_back = move:reverse() -- 建立由快至慢的動作 local move_ease_in = cc.EaseSineIn:create(createSimpleMoveBy()) local move_ease_in_back = move_ease_in:reverse() -- 建立有慢至快的動作 local move_ease_out = cc.EaseSineOut:create(createSimpleMoveBy()) local move_ease_out_back = move_ease_out:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back,createSimpleDelayTime()) -- 運動了喂 grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) kathia:runAction(cc.RepeatForever:create(seq3)) -- 設定標題 Helper.titleLabel:setString("EaseSineIn - EaseSineOut") return layerend------------------------------------- SpriteEaseSineInOut 精靈由慢至快再由快至慢-----------------------------------local function SpriteEaseSineInOut() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() local move_back = move:reverse() -- 由慢至快再由快至慢 local move_ease = cc.EaseSineInOut:create(createSimpleMoveBy()) local move_ease_back = move_ease:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) -- 設定兩個醜八怪的位置 positionForTwo() -- 醜八怪運動啦 grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) Helper.titleLabel:setString("EaseSineInOut action") return layerend------------------------------------- SpriteEaseElastic-----------------------------------local function SpriteEaseElastic() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() local move_back = move:reverse() -- 讓目標動作賦予彈性 ,且以目標動作起點位子賦予彈性 local move_ease_in = cc.EaseElasticIn:create(createSimpleMoveBy()) local move_ease_in_back = move_ease_in:reverse() -- 讓目標動作賦予彈性 ,且以目標動作終點位子賦予彈性 local move_ease_out = cc.EaseElasticOut:create(createSimpleMoveBy()) local move_ease_out_back = move_ease_out:reverse() -- 延遲動作 local delay = createSimpleDelayTime() -- 建立動作序列 local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) -- 三個醜八怪又執行動作了 grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) kathia:runAction(cc.RepeatForever:create(seq3)) -- 設定標題 Helper.titleLabel:setString("Elastic In - Out actions") return layerend------------------------------------- SpriteEaseElasticInOut 讓目標動作賦予彈性 ,且以目標動作起點和終點位子賦予彈性-----------------------------------local function SpriteEaseElasticInOut() -- 建立基礎層 local layer = getBaseLayer() -- 建立簡單移動的動作 local move = createSimpleMoveBy() -- 讓目標動作賦予彈性 ,且以目標動作起點和終點位子賦予彈性,頻率0.3 local move_ease_inout1 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.3) local move_ease_inout_back1 = move_ease_inout1:reverse() -- 讓目標動作賦予彈性 ,且以目標動作起點和終點位子賦予彈性,頻率0.45 local move_ease_inout2 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.45) local move_ease_inout_back2 = move_ease_inout2:reverse() -- 讓目標動作賦予彈性 ,且以目標動作起點和終點位子賦予彈性,頻率0.6 local move_ease_inout3 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.6) local move_ease_inout_back3 = move_ease_inout3:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move_ease_inout1, delay, move_ease_inout_back1, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease_inout2, createSimpleDelayTime(), move_ease_inout_back2, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime()) tamara:runAction(cc.RepeatForever:create(seq1)) kathia:runAction(cc.RepeatForever:create(seq2)) grossini:runAction(cc.RepeatForever:create(seq3)) Helper.titleLabel:setString("EaseElasticInOut action") return layerend------------------------------------- SpriteEaseBounce-----------------------------------local function SpriteEaseBounce() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() local move_back = move:reverse() -- 讓目標動作緩慢開始 local move_ease_in = cc.EaseBounceIn:create(createSimpleMoveBy()) local move_ease_in_back = move_ease_in:reverse() -- 讓目標動作賦予反彈力,且以目標動作結束位子開始反彈 local move_ease_out = cc.EaseBounceOut:create(createSimpleMoveBy()) local move_ease_out_back = move_ease_out:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) -- 煩不煩? grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) kathia:runAction(cc.RepeatForever:create(seq3)) Helper.titleLabel:setString("Bounce In - Out actions") return layerend------------------------------------- SpriteEaseBounceInOut 讓目標動作賦予反彈力,且以目標動作起始與結束位子開始反彈-----------------------------------local function SpriteEaseBounceInOut() -- 得到基礎層 local layer = getBaseLayer() -- 建立簡單移動動作 local move = createSimpleMoveBy() local move_back = move:reverse() -- 讓目標動作賦予反彈力,且以目標動作起始與結束位子開始反彈 local move_ease = cc.EaseBounceInOut:create(createSimpleMoveBy()) local move_ease_back = move_ease:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) positionForTwo() -- 為什麼只用兩個醜八怪? grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) Helper.titleLabel:setString("EaseBounceInOut action") return layerend------------------------------------- SpriteEaseBack-----------------------------------local function SpriteEaseBack() local layer = getBaseLayer() local move = createSimpleMoveBy() local move_back = move:reverse() -- 讓目標動作賦予回力 , 且以目標動作起點位置作為回力點 local move_ease_in = cc.EaseBackIn:create(createSimpleMoveBy()) local move_ease_in_back = move_ease_in:reverse() -- 讓目標動作賦予回力 , 且以目標動作終點位置作為回力點 local move_ease_out = cc.EaseBackOut:create(createSimpleMoveBy()) local move_ease_out_back = move_ease_out:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) kathia:runAction(cc.RepeatForever:create(seq3)) Helper.titleLabel:setString("Back In - Out actions") return layerend------------------------------------- SpriteEaseBackInOut 讓目標動作賦予回力 , 且以目標動作起點和終點位置作為回力點-----------------------------------local function SpriteEaseBackInOut() local layer = getBaseLayer() local move = createSimpleMoveBy() local move_back = move:reverse() -- 讓目標動作賦予回力 , 且以目標動作起點和終點位置作為回力點 local move_ease = cc.EaseBackInOut:create(createSimpleMoveBy()) local move_ease_back = move_ease:reverse() local delay = createSimpleDelayTime() local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) local seq2 = cc.Sequence:create(move_ease,createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) positionForTwo() grossini:runAction(cc.RepeatForever:create(seq1)) tamara:runAction(cc.RepeatForever:create(seq2)) Helper.titleLabel:setString("EaseBackInOut action") return layerend------------------------------------- SpeedTest 速度測試-----------------------------------local SpeedTest_entry = nillocal SpeedTest_action1 = nillocal SpeedTest_action2 = nillocal SpeedTest_action3 = nillocal function altertime(dt) -- 設定速度 SpeedTest_action1:setSpeed(math.random() * 2) SpeedTest_action2:setSpeed(math.random() * 2) SpeedTest_action3:setSpeed(math.random() * 2)endlocal function SpeedTest_onEnterOrExit(tag) if tag == "enter" then SpeedTest_entry = scheduler:scheduleScriptFunc(altertime, 1.0, false) elseif tag == "exit" then scheduler:unscheduleScriptEntry(SpeedTest_entry) endend-- 速度測試local function SpeedTest() local layer = getBaseLayer() --引數1:跳到目標動作位子的所需時間 --引數2:目標位置 --引數3:跳的高度 --引數4跳到目標位置的次數 local jump1 = cc.JumpBy:create(4, cc.p(- s.width + 80, 0), 100, 4) local jump2 = jump1:reverse() -- 反向動作 -- 旋轉動作,引數1:旋轉的時間 引數2:旋轉的角度 0 - 360 local rot1 = cc.RotateBy:create(4, 360 * 2) local rot2 = rot1:reverse() -- 蹦蹦跳跳的動作序列 local seq3_1 = cc.Sequence:create(jump2, jump1) -- 旋轉的動作序列 local seq3_2 = cc.Sequence:create(rot1, rot2) -- 讓多個動作同時執行 local spawn = cc.Spawn:create(seq3_1, seq3_2) -- 速度動作1 SpeedTest_action1 = cc.Speed:create(cc.RepeatForever:create(spawn), 1.0) -- 克隆一個spawn local spawn2 = spawn:clone() -- 速度動作2 SpeedTest_action2 = cc.Speed:create(cc.RepeatForever:create(spawn2), 1.0) local spawn3 = spawn:clone() -- 速度動作3 SpeedTest_action3 = cc.Speed:create(cc.RepeatForever:create(spawn3), 1.0) -- 最後一個了 grossini:runAction(SpeedTest_action2) tamara:runAction(SpeedTest_action3) kathia:runAction(SpeedTest_action1) layer:registerScriptHandler(SpeedTest_onEnterOrExit) Helper.titleLabel:setString("Speed action") return layerendfunction EaseActionsTest() -- 建立場景 local scene = cc.Scene:create() cclog("EaseActionsTest") -- 建立方法表 Helper.createFunctionTable = { SpriteEase, SpriteEaseInOut, SpriteEaseExponential, SpriteEaseExponentialInOut, SpriteEaseSine, SpriteEaseSineInOut, SpriteEaseElastic, SpriteEaseElasticInOut, SpriteEaseBounce, SpriteEaseBounceInOut, SpriteEaseBack, SpriteEaseBackInOut, SpeedTest } -- 新增子節點 scene:addChild(SpriteEase()) -- 新增返回選單 scene:addChild(CreateBackMenuItem()) return sceneend