cocos2d-x——用精灵帧创建动画

该博客介绍了如何在Cocos2d-x中创建动画。首先,初始化CCLayer,然后创建精灵并设置位置。接着,通过循环遍历和处理多帧图片,创建精灵帧动画。利用CCAnimation添加精灵帧,并设置帧间隔时间和播放选项。最后,应用动画到精灵并使其运行,实现无限循环的动画效果。

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

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    //生成第一个精灵

    CCSprite *spr=CCSprite::create("crop1.png");

    spr->setPosition(ccp(200300));

    addChild(spr);

    

    //将每帧图放入动画类对象中。

    CCAnimation *animation=CCAnimation::create();

    for (int i=0; i<4; i++)

    {

        char str[100];

        sprintf(str, "crop%d.png",i+1);

        //getContentSize 得到精灵的宽和高。

        CCRect rect=CCRect(00, spr->getContentSize().width, spr->getContentSize().height);

        //第二个参数指定截取第一个参数指定的图片的一部分。

        CCSpriteFrame *frame=CCSpriteFrame::create(str, rect);//添加精灵帧

        animation->addSpriteFrame(frame);

    }

    //设置帧间隔时间,此参数必须设置,无此参数支画不会播放。

    animation->setDelayPerUnit(1.0f);

    //动画播放完毕后,帧序是否重设为默认第一帧。

   animation->setRestoreOriginalFrame(true);

    //循环次数。

    animation->setLoops(-1);

    

    //形成动画效果

    CCFiniteTimeAction *animate=CCAnimate::create(animation);

    spr->runAction(animate);

    

    return true;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值