cocos讀取plist檔案

CnctSoft發表於2019-05-11

PlistUtils.cpp

#include "PlistUtils.h"

PlistUtils::PlistUtils(void)
{
}

CCRect PlistUtils::getRectFromPlist(const char* fileName,const char* key1,const char* key2,const char* key3){

	CCDictionary* pDictionary=(CCDictionary*)CCDictionary::createWithContentsOfFile(fileName);
	CCDictionary* content1=(CCDictionary*)pDictionary->objectForKey(key1);
	CCDictionary* content2=(CCDictionary*)content1->objectForKey(key2);
	CCString* object=(CCString*)content2->objectForKey(key3);
	
	const char* s=object->getCString();
	const short l=object->length();	

	char temp[4];
	short tempPosition=0;
	short tempRect[4];
	short tempRectPosition=0;

	for (int i=0;i<l;i++){
		if((*(s+i)=='{'||*(s+i)=='}'||*(s+i)==' ')&&i!=l-1){
			continue;
		}else if(*(s+i)==','||i==l-1){
			tempRect[tempRectPosition]=atoi(temp);
			//CCLog("%d",tempRect[tempRectPosition]);
			tempRectPosition++;

			tempPosition=0;
			temp[0]='\0';
			temp[1]='\0';
			temp[2]='\0';
			temp[3]='\0';
			
		}else{
			temp[tempPosition]=*(s+i);
			tempPosition++;
		}
	}



	return CCRectMake(tempRect[0],tempRect[1],tempRect[2],tempRect[3]);
}

/*通過plist檔案
@fileName plist檔案
@resBigPicName 組合圖
@resSmallPicName 小圖名稱*/
CCSprite* PlistUtils::getSpriteFromPlist(const char* fileName, const char* resBigPicName, const char* resSmallPicName){
	CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();	
	frameCache->addSpriteFramesWithFile(fileName, resBigPicName);
	CCSpriteFrame* spriteFrame = frameCache->spriteFrameByName(resSmallPicName);
	CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame);
	return sprite;
}
/*清除CCSpriteFrameCache*/
void  PlistUtils::spriteCacheDispose(){
	CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
	frameCache->autorelease();
	frameCache->retain();
}



PlistUtils.h
#ifndef __PLIST_UTILS_H__
#define __PLIST_UTILS_H__

#include "cocos2d.h"
USING_NS_CC;
using namespace cocos2d;

/*讀取plist工具類*/
class PlistUtils
{
public:
	PlistUtils(void);
	/*獲取plist 中資源所在的CCRECT*/
	static CCRect getRectFromPlist(const char* fileName,const char* key1,const char* key2,const char* key3);
	/*
		讀取指定精靈
		@fileName plist路徑
		@resBigPicName 大圖地址
		@resSmallPicName 小圖地址
	*/
	static CCSprite* getSpriteFromPlist(const char* fileName, const char* resBigPicName, const char* resSmallPicName);
	static void spriteCacheDispose();
};
#endif




內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章