cocos-lua学习笔记(九)动作
下面是所有动作相关的接口,摘自《cocos2d-Lua基础》
瞬时动作:
1.place:节点放置到某一位置
2.Flipx与Flipy:只能用于精灵,沿x或y轴反转
3.Show与Hide 显示和隐藏节点
4.CallFunc 动作结束的,启动的逻辑
有限时间动作
1.MoveTo 与 MoveBy
2.JumTo 与JumpBy
3.BezierTo 与BezierBy
4.ScaleTo与ScaleBy
5.RotateTo与RotateBy
6.FadeIn、FadeOut和FadeTo
7.TintTo 与TintBy
8.Blink
9.Animation
复合动作
1.DelayTime
2.Repeat与RepeatForever
3.Spawn
4.Sequence
5.Follow
变速动作
1.Speed
2.ActionBase ->这里有很多图
节点动作接口
node:runAction()
node:stopAction()
node:stopAllActions
node:stopAction(action)
node:stopActionByTag(tag)
node:setTag(tag)
action:setTag(tag)
node:getActionByTag(tag)
action:stopActionByTag(tag)
具体的接口怎么使用,直接上一个Demo展示
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
function MainScene:ctor()
self.backgroundLayer = display.newColorLayer(cc.c4f(128,128,128,255))
self.backgroundLayer:addTo(self)
-- preload frames to cache
display.addSpriteFrames("grossini-aliases.plist", "grossini-aliases.png")
-- run actions: 1 to 9
self:run(1)
end
function MainScene:run(actionNum)
self["action" .. actionNum](self)
end
--[[
Follow
]]
function MainScene:action1()
-- 用在层跟随精灵中,固定摄像机到精灵身上
local sprite1 = display.newSprite("1.png")
sprite1:center()
local move_left = cc.MoveBy:create(1.5, cc.p(display.width / 2, 0))
local move_right = cc.MoveBy:create(3, cc.p(- display.width, 0))
local seq = cc.Sequence:create(move_left, move_right, move_left)
local rep = cc.RepeatForever:create(seq)
sprite1:runAction(rep)
sprite1:addTo(self.backgroundLayer)
self.backgroundLayer:runAction(cc.Follow:create(sprite1))
end
--[[
MoveBy
]]
function MainScene:action2()
local sprite2 = display.newSprite("2.png")
:center()
:addTo(self.backgroundLayer)
-- true 翻转,false 重置为最初的状态
local flipxAction = cc.FlipX:create(true)
local moveTo = cc.MoveBy:create(1, cc.p(-300, 0))
local action = cc.Sequence:create(moveTo, flipxAction, moveTo:reverse())
sprite2:runAction(action)
end
--[[
Hide
]]
function MainScene:action3()
local hideAction = cc.Hide:create()
local moveTo = cc.MoveTo:create(1.5, cc.p(60, 60))
local action = cc.Sequence:create(moveTo, hideAction)
local sprite1 = display.newSprite("1.png")
:center()
:addTo(self.backgroundLayer)
:runAction(action)
end
--[[
CallFunc
]]
function MainScene:action4()
-- 判断顺序动画执行结束
local callback = cc.CallFunc:create(function() print("In callback function") end)
local moveTo = cc.MoveTo:create(2, cc.p(0, 0))
local action = cc.Sequence:create(moveTo, callback)
local sprite1 = display.newSprite("1.png")
:center()
:addTo(self.backgroundLayer)
:runAction(action)
end
--[[
贝塞尔曲线
]]
function MainScene:action5()
local action = cc.BezierTo:create(2, {cc.p(display.right, display.top), cc.p(200, 200), cc.p(50, 100)})
local sprite1 = display.newSprite("1.png")
:pos(0, 0)
:addTo(self.backgroundLayer)
:runAction(action)
end
--[[
FadeTo
]]
function MainScene:action6()
local action = cc.FadeTo:create(2, 0)
local sprite1 = display.newSprite("1.png")
:center()
:addTo(self.backgroundLayer)
:runAction(action)
end
--[[
帧动画
]]
function MainScene:action7()
local frames = display.newFrames("grossini_dance_%02d.png", 1, 14)
local animation = display.newAnimation(frames, 0.2)
local animate = cc.Animate:create(animation)
local sprite1 = display.newSprite("#grossini_dance_01.png")
:center()
:addTo(self.backgroundLayer)
:runAction(animate)
end
--[[
延迟动作
]]
function MainScene:action8()
local move = cc.MoveBy:create(1, cc.vertex2F(150,0))
local action = cc.Sequence:create(move, cc.DelayTime:create(2), move:reverse())
local sprite1 = display.newSprite("1.png")
:center()
:addTo(self.backgroundLayer)
:runAction(action)
end
--[[
变速动作
]]
function MainScene:action9()
local action = cc.EaseSineOut:create(cc.MoveBy:create(2, cc.p(300, 0)))
local sprite1 = display.newSprite("1.png")
:center()
:addTo(self.backgroundLayer)
:runAction(action)
end
function MainScene:onEnter()
end
function MainScene:onExit()
end
return MainScene