【Cocos2d-x Lua笔记七】Action动作

时间:2023-02-08 20:35:23

Action常会修改对象的一些属性,如位置,旋转,缩放等。

常用的动作

1.简单动作

MoveTo——移动动作

MoveBy

 

RotateTo——旋转动作

RotateBy

 

ScaleTo——缩放动作

Scaleby

 

FadeIn——淡入,修改透明度的值(0~255之间

FadeOut——淡出

 

TintTo——色调,使节点的NodeRGB从当前值改变到用户设置值

TintBy

 

简单的说to是绝对的,by是相对的。。By相当于这个节点是相对的。To是绝对的,意思是它并不考虑到这个节点的当前状态。

 

2.动作序列

Sequence可以使一系列的动作对象按顺序执行,可以将最后一个动作设置为回调动作,当动作完成时就能调用相关函数。cc.Sequence:create(action1action2,action3,callfunc)

Spawn使所有动作是同时执行的。

Reverse调用reserve()函数使动作逆序执行。Action:reverse()

 

3.动作回调

cc.CallFunc:create(hander, value) -- hander : 执行的回调函数-- value  : 传递给回调函数的可选参数,必须为一个table  4.缓动函数使动作变化的幅度进行相应非线性变化【Cocos2d-x Lua笔记七】Action动作

EaseIn/EaseOut: 动作由慢变快/动作由快变慢

EaseInOut:动作由慢变快再由快变慢

EaseExponentialIn:动作由慢变极快

EaseExponentialOut:动作由极快变慢

EaseExponentialInOut:动作由慢至极快再由极快边慢

EaseSineIn:动作由快变慢

EaseSineOut:动作由慢变快

EaseSineInOut:精灵由慢至快再由快至慢

EaseElasticIn:让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性

EaseElasticOut:让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性

EaseElasticInOut:让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性

EaseBounceIn:让目标动作缓慢开始

EaseBounceOut:让目标动作赋予反弹力,且以目标动作结束位子开始反弹

EaseBounceInOut:让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹

EaseBackIn:让目标动作赋予回力 , 且以目标动作起点位置作为回力点

EaseBackOut:让目标动作赋予回力 , 且以目标动作终点位置作为回力点

EaseBackInOut:让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点

JumpBy/JumpTo:跳的动作

RotateBy/RotateTo:旋转的动作

Spawn:让多个动作同时执行

Speed:让目标运行速度加倍

 

范例:

通过Action使开始按钮循环缩放,当按下按钮时停止缩放动作

【Cocos2d-x Lua笔记七】Action动作

local GameState=require(cc.PACKAGE_NAME .. ".cc.utils.GameState")
require(cc.PACKAGE_NAME..".cc.ui.UIPushButton")



--创建游戏主页场景
local HomeScene = class("HomeScene", function()
return display.newScene("HomeScene")
end)


GameData={}

HomeScene.CHECKBOX_BUTTON_IMAGES={
off="Images/menu_sound-sheet1.png",
on="Images/menu_sound-sheet0.png",
}

HomeScene.PUSH_BUTTON_IMAGES={
normal="Images/day_start_btn.png",
pressed="Images/day_start_btn_pressed.png",
disabled ="Images/day_start_btn_pressed.png",
}

function HomeScene:ctor()
self:addElement()
self:addMenuElement()
end

function HomeScene:addElement()

self.MainBg=display.newTilesSprite("Background/day_buttom.png",cc.rect(display.left,display.bottom,display.width,display.height)):addTo(self)
--align(target, anchorPoint, x, y)
self.MainBottomBg=display.newSprite("Background/day_tree_buttom.png"):align(display.BOTTOM_CENTER,display.cx,display.bottom):addTo(self)
self.MainTitleBg=display.newSprite("Background/day_title_buttom.png"):align(display.TOP_CENTER,display.cx,display.top-80):addTo(self)
self.MainRabbitBg=display.newSprite("Background/day_rabbit_buttom.png"):align(display.BOTTOM_CENTER,220,display.bottom+20):scale(0.8):addTo(self)
end



function HomeScene:addMenuElement()
self:initGameState()
--开始按钮
self.startBtn=cc.ui.UIPushButton.new(HomeScene.PUSH_BUTTON_IMAGES,{scale9=true})
:setButtonLabel("normal",cc.ui.UILabel.new({
UILabelType=1,
text="开始",
size=62,
font="Font/hero.fnt"
}))
:setButtonLabel("pressed",cc.ui.UILabel.new({
UILabelType=1,
text="开始",
size=62,
font="Font/hero.fnt",
color=cc.c4b(255,64,64,100)
}))
:setButtonLabel("disabled",cc.ui.UILabel.new({
UILabelType=1,
text="开始",
size=62,
font="Font/hero.fnt",
color=cc.c4b(0,0,0,100)
}))
:onButtonClicked(function(event)
self:playSound(GAME_BGM.buttonClickSound)
event.target:stopAllActions()
end)
:align(display.CENTER,display.cx,display.cy)
:addTo(self)
self:ActionScale()
--设置音乐开关
local function updateSoundCheckBox(checkbox)
local state= ""
if checkbox:isButtonSelected() then
state="on"
else
state="off"
end
GameData.soundState=state
GameState.save(GameData)
end
--声音按钮
self.soundCheckBox=cc.ui.UICheckBoxButton.new(HomeScene.CHECKBOX_BUTTON_IMAGES)
:onButtonStateChanged(function(event)
updateSoundCheckBox(event.target)
end)
:align(display.BOTTOM_RIGHT,display.width-40,display.bottom+40)
:scale(0.9)
:addTo(self)
if GameData.soundState =="on" then
self.soundCheckBox:setButtonSelected(true)
else
self.soundCheckBox:setButtonSelected(false)
end

end

function HomeScene:initGameState()
GameState.init(function(param)
local returnValue=nil
if param.errorCode then
printError("errorCode",param.errorCode)
else
if param.name==GAME_STATE_ENCRYPTION_OPERATION_SAVE then
local str=json.encode(param.values)
str=crypto.encryptXXTEA(str,GAME_STATE_ENCRYPTION_XXTEA)
returnValue={data=str}
elseif param.name==GAME_STATE_ENCRYPTION_OPERATION_LOAD then
local str=crypto.decryptXXTEA(param.values.data,GAME_STATE_ENCRYPTION_XXTEA)
returnValue=json.decode(str)
end
end
return returnValue
end,GAME_STATE_ENCRYPTION_FILENAME,GAME_STATE_ENCRYPTION)
if io.exists(GameState.getGameStatePath()) then
GameData=GameState.load()
print("savePath:"..GameState.getGameStatePath())
end
local soundState=GameData.soundState
if soundState==nil then
GameData.soundState="on"
GameState.save(GameData)
end
end

--开始按钮缩放动画
function HomeScene:ActionScale()
self.startBtn:runAction(cc.Sequence:create(
cc.EaseInOut:create(cc.ScaleTo:create(1.2,1.2,1.2),1.2),
cc.ScaleTo:create(1.2,1,1),
cc.CallFunc:create(function() self:ActionScale() end)))
end

--播放音效
function HomeScene:playSound(sound)
if GameData.soundState=="on" then
audio.playSound(sound)
else
print("sound","off")
end
end

function HomeScene:onEnter()
end

function HomeScene:onExit()
end


return HomeScene