您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x之TexturePacker使用

2013-05-25 20:26 344 查看
TexturePacker用来把大量图片批量导入,简化开发,直接贴代码

用来生成plist的原始图为A1_0.png到A1_3.png

   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.plist中的第一张图片名字创建一个精灵
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));

this->addChild(pSprite);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: