cocos2d——用精灵帧创建动画

来源:互联网 发布:mp4视频修复软件 编辑:程序博客网 时间:2024/06/05 23:44

boolHelloWorld::init()

{

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

    // 1. super init first

   if ( !CCLayer::init() )

    {

        returnfalse;

    }

   //生成第一个精灵

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

    spr->setPosition(ccp(200,300));

   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(0,0, 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);

    

    returntrue;

}