cocos2d-lua3.7组件篇(二)-两帧图片互相跳动

本文介绍了一种使用两帧图片实现动画切换效果的方法。通过缓存动画对象并加载两张图片,设置循环播放及延迟时间,实现了动画效果。文章还展示了如何通过按钮触发动画播放。

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

组件:两帧图片互相切换的动画。

 

 

实现如下效果:

 

 

 

 

1.缓存动画对象

2.加载两帧图片

 

local MenuScene = class("MenuScene", function()
    return display.newScene("MenuScene")
end)

function MenuScene:ctor()


            local animate = cc.Animation:create()
            animate:addSpriteFrameWithFile("dog.png")
            animate:addSpriteFrameWithFile("dog2.png")
                animate:setLoops(-1)
            animate: setDelayPerUnit(2.8 / 14.0)

    display.setAnimationCache("haha",animate)

    local btn = ccui.Button:create("button/aaa_N.png", "", "", 0)  
        :pos(display.cx, 100)  
        :addTo(self)  
        --按钮文字  
        btn:setTitleText("按钮")  
        --字体颜色  
        btn:setTitleColor(cc.c3b(255, 255, 255))  
        --按钮的回调函数  
        btn:addTouchEventListener(function(sender, eventType)  
        if (2== eventType)  then  
            
            

            local action = cc.Animate:create(display.getAnimationCache("haha"))

            local sprite1 = display.newSprite("dog2.png")  
                :center()  
                :addTo(self)  
                :runAction(action)  

        end  
    end)  

--getAnimationCache





end

function MenuScene:onEnter()
    print("enter")
end

function MenuScene:onExit()
end

return MenuScene

 

 

 

 

 

转载于:https://www.cnblogs.com/hiwoshixiaoyu/p/10034950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值