plist文件为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>texture</key>
<dict>
<key>width</key>
<integer>1024</integer>
<key>height</key>
<integer>1024</integer>
</dict>
<key>frames</key>
<dict>
<key>main_bg_c@2x.png</key>
<dict>
<key>id</key>
<integer>1002</integer>
<key>x</key>
<integer>1</integer>
<key>y</key>
<integer>1</integer>
<key>width</key>
<integer>640</integer>
<key>height</key>
<integer>760</integer>
<key>offsetX</key>
<real>0</real>
<key>offsetY</key>
<real>0</real>
<key>originalWidth</key>
<integer>640</integer>
<key>originalHeight</key>
<integer>760</integer>
</dict>
<key>main_trees_a@2x.png</key>
<dict>
........ </dict>
<key>main_trees_b@2x.png</key>
<dict>
...... </dict>
</dict>
</dict>
</plist>
读取方法为:
//读取plist数据
CCRect Tools::getRectFromPlist(const char* fileName,const char* key1,const char* key2,const char* x,const char* y,const char* width,const char* height){
CCDictionary* pDictionary=(CCDictionary*)CCDictionary::createWithContentsOfFile(fileName); //加载文件
CCDictionary* content1=(CCDictionary*)pDictionary->objectForKey(key1);
CCDictionary* content2=(CCDictionary*)content1->objectForKey(key2);
CCString* _x=(CCString*)content2->objectForKey(x);
CCString* _y=(CCString*)content2->objectForKey(y);
CCString* _width=(CCString*)content2->objectForKey(width);
CCString* _height=(CCString*)content2->objectForKey(height);
//return CCRectMake(0,0,100,100);
return CCRectMake(_x->intValue(),_y->intValue(),_width->intValue(),_height->intValue());
}
然后会得到一个CCRect,当然这样的方式在cocos2d中显得笨重。
----------------------------------------------------------------------------------------------------------------------------------------------
下面方法会显得更合适
//将BIRD_LIST加入缓冲区
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(BIRD_LIST);
[BIRD_LIST为plist的路径]
只要之前加入了plist入缓冲区,在任意一个文件下都可以直接使用下面这句话,获取其内部图片。
CCSprite *pSprite=CCSprite::createWithSpriteFrameName(“plist中key中显示的图片名”);
会得到一个切好的小图片。
//用它切换图片
1.// 首先载入贴图集
2.CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWithFile("bird.png");
3.this->addChild(spriteBatch);
4.CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("bird.plist");
5.// 生成Sprite
6.CCSprite *firstSprite=CCSprite::spriteWithSpriteFrameName("headup.png");
7.//需要更换图片时
8.CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("yellow_bird.png");
9.firstSprite->setDisplayFrame(frame);
当然帧动画用它再合适不过。
CCSprite* stageLevel=CCSprite::createWithSpriteFrameName(stage_level_ready);
stageLevel->setPosition(ccp(stageLevel->getContentSize().width/2+130,stageLevel->getContentSize().height));
stageLevelReady->setTag(1002);
pSender->getChildByTag(1001)->addChild(stageLevel);
CCSpriteFrame *stageLevelReady=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(stage_level_ready);
CCSpriteFrame *stageLevelLevel=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(stage_level_level);
CCSpriteFrame *stageLevelStart=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(stage_level_start);
CCArray* pArray = CCArray::createWithCapacity(3);//此处不能直接用create
pArray->addObject(stageLevelReady);
pArray->addObject(stageLevelLevel);
pArray->addObject(stageLevelStart);
CCAnimation* animation= CCAnimation::createWithSpriteFrames(pArray,duration);
CCAnimate* animate=CCAnimate::create(animation);
stageLevel->runAction(CCRepeat::create(animate,1));