转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25168047
程序效果图:
1、下载安装TexturePackerGUI
地址:http://www.codeandweb.com/texturepacker
2、制作plist文件和png图片
打开TexturePackerGUI,将动画素材拖拽至TexturePackerGUI图片区,填写Output路径及名称,点击工具栏Publish,生成plist、png文件,然后将此2文件放至项目Resources下:
编敲代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); //装载plist文件 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("izmuo.plist", "izmuo.png"); CCArray *array = CCArray::create(); char ptr[10]={0}; //获取每一帧动画,遍历图片名字 for (int i=1; i<10; i++) { sprintf(ptr, "%d.png", i); CCLOG("ptr: %s",ptr); CCSpriteFrame *spriteframe = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(ptr); array->addObject(spriteframe); } //依据帧序列表创建动画对象,指定播放速度 CCAnimation *animation = CCAnimation::createWithSpriteFrames(array,0.2f); //依据动画创建动画动作 CCAnimate *animate = CCAnimate::create(animation); CCSprite *sprite = CCSprite::create(); //開始运行动作 sprite->setScale(0.5); sprite->runAction(CCRepeatForever::create(animate)); sprite->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); this->addChild(sprite); return true; }