使用plist创建精灵并执行动画

 素材图:

然后使用Texture Packer放到一张图片上,并生成plist文件


接下来直接上代码

        CCSize size = CCDirector::sharedDirector()->getWinSize();  //获得屏幕的尺寸

        CCArray* pArray = CCArray::createWithCapacity(4);
        CCSpriteFrameCache* pCache = CCSpriteFrameCache::sharedSpriteFrameCache();  //获得SpriteFrame的缓存
        pCache->addSpriteFramesWithFile("attack.plist");
        CCSprite* pSprite = CCSprite::createWithSpriteFrameName("A1_0.png");  //用attack.png中的第一张图片创建一个精灵
        pSprite->setPosition(ccp(size.width/2,size.height/2));  //设置精灵的座标

        
        char name[20];
        for(int i=0; i<4; i++)  //将plist中的图片都加载,放到array中
        {
            sprintf(name, "A1_%d.png", i);
            CCSpriteFrame* pframe = pCache->spriteFrameByName(name);
            pArray->addObject(pframe);
        }
        
        CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames(pArray,0.2f);  //用array中的spriteframe创建一个animation,每帧间隔0.2s
        CCAnimate* panimate = CCAnimate::create(pAnimation);
        
        pSprite->runAction(CCRepeatForever::create(panimate));


运行结果就是


在mac下面qq截图不能截动态的,就给个样子,大家自己写个demo试试就可以看到效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章