cocos2d+TexturePackerGUI动画制作

转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25168047

程序效果图:

cocos2d+TexturePackerGUI动画制作_第1张图片


1、下载安装TexturePackerGUI

地址:http://www.codeandweb.com/texturepacker

2、制作plist文件和png图片

打开TexturePackerGUI,将动画素材拖拽至TexturePackerGUI图片区,填写Output路径及名称,点击工具栏Publish,生成plist、png文件,然后将此2文件放至项目Resources下:

cocos2d+TexturePackerGUI动画制作_第2张图片


编写程序:

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;
}



你可能感兴趣的:(Cocos2d-x,Cocos2d-x从零到精通)