Cocos2d-x Lua action

本文是关于Cocos2d-x游戏引擎中使用Lua进行动作控制的教程,详细介绍了如何在Cocos2d-x环境中通过Lua脚本实现游戏对象的动作效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎各位童鞋转载,转载请注明出处:http://blog.youkuaiyun.com/song_hui_xiang

作者新浪微博:http://weibo.com/u/3168848533

作者腾讯微博:http://t.qq.com/song_huixiang

local size = CCDirector:sharedDirector():getWinSize()
local layer = CCLayer:create()

local man = CCSprite:create("man.png")
local blackgirl = CCSprite:create("blackgirl.png")
local whitegirl = CCSprite:create("whitegirl.png")

blackgirl:setPosition(ccp(size.width*0.5,size.height*0.66))
whitegirl:setPosition(ccp(size.width*0.5,size.height*0.5))
man:setPosition(ccp(size.width*0.5,size.height*0.33))


layer:addChild(man,1)
layer:addChild(blackgirl,2)
layer:addChild(whitegirl)


local function ActionManual()
    man:setScaleX(2.5)
    --负值得到镜像
    man:setScaleY(-1)
    --man:setScale(-1)
    man:setOpacity(128)
    
    blackgirl:setRotation(120)
    --blackgirl:setRotation(-120)
    blackgirl:setColor(ccc3(255,0,0))
    whitegirl:setColor(ccc3(0,0,255))
end
--ActionManual()

--移动动画
local function ActionMove()
    local actionBy = CCMoveBy:create(2,ccp(100,100))
    local actionByBack = actionBy:reverse()
    
    man:runAction(CCMoveTo:create(1,ccp(size.width*0.5-100,size.height*0.33-100)))
    whitegirl:runAction(CCSequence:createWithTwoActions(actionBy,actionByBack))
    blackgirl:runAction(CCMoveTo:create(1,ccp(40,40)))
    --对于CC...To的动作,reverse()是不支持的
    --blackgirl:runAction(CCSequence:createWithTwoActions(actionTo,actionTo:reverse())) 
end
--ActionMove()

--缩放动画
local function ActionScale()
    --[[
    local scaleTo = CCScaleTo:create(2,5)
    local scaleTo2 = CCScaleTo:create(2,1)
    whitegirl:runAction(CCSequence:createWithTwoActions(scaleTo,scaleTo2))
    
    local scaleBy = CCScaleBy:create(2,2.5)
    --local scaleBy2 = CCScaleBy:create(2,1)
    local scaleBy2 = scaleBy:reverse()
    blackgirl:runAction(CCSequence:createWithTwoActions(scaleBy,scaleBy2))
    --]]
    
    local scaleTo = CCScaleTo:create(2,0.5)
    local scaleBy = CCScaleBy:create(2,1,10)
    local scaleBy2 = CCScaleBy:create(2,5)
    --对于CC...To的动作,reverse()是不支持的
    --local scaleTo2 = scaleTo:reverse()
    --man:runAction(CCSequence:createWithTwoActions(scaleTo,scaleTo2))
    man:runAction(scaleTo)
    whitegirl:runAction(CCSequence:createWithTwoActions(scaleBy,scaleBy:reverse()))
    blackgirl:runAction(CCSequence:createWithTwoActions(scaleBy2,scaleBy2:reverse()))
end
--ActionScale()

--旋转动画
local function ActionRotate()
    local rotateTo = CCRotateTo:create(2,45)
    local rotateTo2 = CCRotateTo:create(2,0)
    man:runAction(CCSequence:createWithTwoActions(rotateTo,rotateTo2))
    --如果CC...To的什么动作执行了reverse(),则控制台报错如下:Cocos2d: [LUA ERROR] ASSERT FAILED ON LUA EXECUTE: CCIntervalAction: reverse not implemented.
    --man:runAction(CCSequence:createWithTwoActions(rotateTo,rotateTo:reverse()))
    
    local rotateBy = CCRotateBy:create(2,360)
    whitegirl:runAction(CCSequence:createWithTwoActions(rotateBy,rotateBy:reverse()))
    
    local rotateTo3 = CCRotateTo:create(2,-45)
    local rotateTo4 = CCRotateTo:create(2,0)
    blackgirl:runAction(CCSequence:createWithTwoActions(rotateTo3,rotateTo4))
end
--ActionRotate()


--扭曲动作:这个动画是我在本节中遇到的最难解释的动画。我想了好久该怎么说,却仍然有些晦涩。扭曲动画可以理解为对精灵的两对对角分别进行X,Y方向的拉伸扭曲到目标角度的动画。
local function ActionSkew()
    local skewTo = CCSkewTo:create(2,40,40)
    local skewTo2 = CCSkewTo:create(2,0,0)
    man:runAction(CCSequence:createWithTwoActions(skewTo,skewTo2))
    --man:removeFromParentAndCleanup(true)
    
    local skewBy = CCSkewBy:create(2,50,-50)
    local skewBy2 = skewBy:reverse()
    blackgirl:runAction(CCSequence:createWithTwoActions(skewBy,skewBy2))
    
    local shewBy3 = CCSkewBy:create(2,0,90)
    whitegirl:runAction(CCSequence:createWithTwoActions(shewBy3,shewBy3:reverse()))
end
ActionSkew()


local scene = CCScene:create()
scene:addChild(layer)
CCDirector:sharedDirector():runWithScene(scene)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值