使用Tiiled工具制作瓦片地图,生成xx.tmx文件。把文件和图块资源图片放到项目Resources文件夹下。cpp init()函数下加入代码
CCTMXTiledMap *map = CCTMXTiledMap::create("0.tmx");
addChild(map);
使用TexturePacker工具打包图片资源,生成image.png和image.plist文件。拷贝文件到项目Resources文件夹下。cpp init()函数下加入代码
<span style="font-size:18px;">//载入精灵帧缓存
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("image.plist","image.png");
//通过文件名建立精灵
CCSprite* sprite0 = CCSprite::createWithSpriteFrameName("rpg_02.png");
sprite0->setPosition(ccp(200,200));
addChild(sprite0);
//使用vector容器创建动画帧数组
Vector<CCSpriteFrame*> downArray;
//格式化字符串,通过名称加载精灵帧到容器
for(int index=1;index<4;++index)
{
CCLOG(CCString::createWithFormat("%s%d.png","rpg_0",index)->getCString());
std::string name = CCString::createWithFormat("%s%d.png","rpg_0",index)->getCString();
downArray.pushBack(cache->spriteFrameByName(name));
}
//创建animate,精灵执行动作。
CCAnimation* animation = CCAnimation::createWithSpriteFrames(downArray,0.2f);
CCAnimate* animate = CCAnimate::create(animation);
downsprite->runAction(CCRepeatForever::create(animate));</span>