內容介紹:
6、數據操作 (1)、計時器 schedule(schedule_selector(HelloWorld::testJ),1); //void HelloWorld::testJ(float f){} 每隔1秒執行下這個函數 (2)、用戶首選項默認數據讀寫 UserDefault::getInstance()->setStringForKey("data","hellow"); UserDefault::getInstance()->getStringForKey("data","default"); (3)、文件讀寫 auto fu = FileUtils::getInstance(); FILE *f = fopen(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()).c_str(),"w"); fprintf(f,"helloworldn"); fclose(f); Data d = fu->getDataFromFile(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()).c_str()); log("%s",d.getBytes()); (4)、讀取plist文件 FileUtils *fu = FileUtils::getInstance(); ValueMap vm = fu->getValueMapFromFile("data.plist"); log("%s",vm["name"].asString().c_str()); 【完整例子:】 ValueMap levelinfo=FileUtils::getInstance()->getValueMapFromFile("level01.plist"); ValueMap mapinfo=levelinfo["mapinfo"].asValueMap(); ValueMap linfo=levelinfo["levelinfo"].asValueMap(); CCLOG("level info=%s",mapinfo["mapfile"].asString().c_str()); CCLOG("level info=%d",linfo["money"].asInt()); ValueVector group=linfo["group"].asValueVector(); CCLOG("一共有怪物波數%ld",group.size()); for (int i=0; i<group.size(); i++) { long npccount= group.at(i).asValueVector().size(); ValueVector nowgroup=group.at(i).asValueVector(); for (int j=0; j<npccount; j++) { CCLOG("npc group %d type %d,hp %d",i+1,nowgroup.at(j).asValueMap()["npctype"].asInt() ,nowgroup.at(j).asValueMap()["npchp"].asInt()); } } (5)、讀取XML文件 #include <tinyxml2/tinyxml2.h> auto doc = new tinyxml2::XMLDocument(); doc->Parse(FileUtils::getInstance()->getStringFromFile("data.xml").c_str); auto root = doc->RootElement(); for(auto e=root->FirstChildElement();e;e=e->NextSiblingElemeng()){ std::string str; for(auto attr = e->FirstAttribute();attr;attr=e->Next()){ str += attr->Name(); str += ":"; str += attr->value(); str += ","; } log("%s",str.c_str()); } (6)、讀取json文件 #include <json/document.h> rapidjson::Document d; d.Parse<0>(FileUtils::getInstance()->getStringFromFile("date.json").c_str()); log(%s",d[(int)0]["name"].GetString()); 7、背景音樂和音效設置 #include "SimpleAudioEngine.h" using namespace CocosDenshion; SimpleAudioEngine::getInstance()->playBackgroundMusic("bg.mid");//加載播放背景音樂 SimpleAudioEngine::getInstance()->pauseBackgroundMusic();//停止背景音樂 SimpleAudioEngine::getInstance()->resumeBackgroundMusic();//恢復背景音樂 SimpleAudioEngine::getInstance()->playEffect("eat.wav");//添加音效 8、加載動畫 auto cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("grossini_blue.plist"); Vector<SpriteFrame*> animFrames(15); char str[100]; for(int i = 1; i <= 4; i++) { sprintf(str, "grossini_blue_%02d.png",i); auto frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } auto animation = Animation::createWithSpriteFrames(animFrames, 0.2f); auto flowSprite = Sprite::create(); flowSprite->runAction(RepeatForever::create(Animate::create(animation))); this->addChild(flowSprite); flowSprite->setPosition(ccp(200, 200)); 手機橫豎屏設置 1、android AndroidManifest.xml文件中, screenOrientation="landscape" 爲橫屏, screenOrientation="portrait"爲豎屏 2、iOS - (NSUInteger) supportedInterfaceOrientations{ #ifdef __IPHONE_6_0 // 橫屏顯示 // return UIInterfaceOrientationMaskLandscape; // 豎屏顯示 return UIInterfaceOrientationMaskPortrait; #endif } 設備屏幕分辨率自適應 CCSize frameSize = glview->getFrameSize(); //這填寫的就是一般你作爲背景圖片的那種圖片的大小,適配的原理就是放到和縮小,而以什麼爲參照,當然就是 //以最大的那張圖片爲參照,什麼圖片最大,當然是背景圖片了,以後美工做圖的時候用的就是以下的這個尺寸 CCSize winSize=CCSize(480,320); //將寬和高做一個比,通過這個比,來具體的調整邏輯分辨率的大小 float widthRate = frameSize.width/winSize.width; float heightRate = frameSize.height/winSize.height; //如果是if中的語句,說明邏輯的高度有點大了,就把邏輯的高縮小到和寬度一樣的比率 if (widthRate > heightRate) { //裏邊傳入的前倆個參數就是邏輯分辨率的大小,也就是通過getWinSize()得到的大小 glview->setDesignResolutionSize(winSize.width, winSize.height*heightRate/widthRate, kResolutionNoBorder); } else { glview->setDesignResolutionSize(winSize.width*widthRate/heightRate, winSize.height, kResolutionNoBorder); } 有關const成員、static成員、const static成員的初始化: 1、const成員:只能在構造函數後的初始化列表中初始化 2、static成員:初始化在類外,且不加static修飾 3、const static成員:類只有唯一一份拷貝,且數值不能改變。因此,可以在類中聲明處初始化,也可以像static在類外初始化 項目移植到android平臺 1、在VS2012中吧代碼調試沒問題 2、使用ADT eclipse工具打開導入項目的proj.android到工具中 3、設置環境變量 android_sdk_root ant_root java_home ndk_root 4、解決android項目庫的加載 E:feijiMyCppGamecocos2dcocosplatformandroidjavasrc 目錄下的org拷貝到 proj.android E:feijiMyCppGameproj.androidsrc 目錄下覆蓋 5、解決eclipse中adriod的版本,右鍵屬性項目 -》 android勾選android4.4->移除下面的library 6、在命令行進入項目的跟目錄 執行: cocos compile -p android 進行項目編譯 autoRelease、retain和release函數 1:那倒底什麼時候要retain? 很簡單,當你把一個對象讓他作爲成員變量時,並且沒有把對象addChild到另外一個對象時,就需要調用retain函數。 Retain的意思是保持引用,也就是說,如果想保持某個對象的引用,避免它被Cocos2d-x釋放,那就要調用對象的retain函數。 一旦調用對象的autoRelease函數,那麼這個對象就被Cocos2d-x的內存管理機制給盯上了,如果這個對象沒人認領,那就等着被釋放吧 addChild函數就是導致大家混亂的兇手了,addChild函數會調用對象的retain函數,爲什麼它要調用對象的retain函數呢?因爲你都把對象送給它當孩子了,它當然要認領這個對象了! 進度條: Sprite *psSprite2 = Sprite::create("loadBar.png"); progresstime2 = CCProgressTimer::create(psSprite2); //初始化CCProgressTimer progresstime2->setType(ProgressTimer::Type::BAR);//設置類型 progresstime2->setMidpoint(Point(0, 0));//設置中心點,0.5,0.5是中間開始加 0,0從左邊開始加 1,1從右邊開始加 progresstime2->setBarChangeRate(Point(1, 0));//設置進度條的長度和高度開始變化的大小 progresstime2->setPercentage(0);//初始化進度條的值 progresstime2->setAnchorPoint(Point(0.5f, 0.5f));//設置進度條的錨點 schedule(schedule_selector(MapScene::gobar)); 在計時器中改變進度條的值:progresstime2->setPercentage(i); 10 20 30 40 ...100 tiled地圖的使用: map = TMXTiledMap::create("map.tmx");//創建一個地圖 //獲得地圖對象路徑上的所有點 TMXObjectGroup *group = map->objectGroupNamed("obj"); std::vector<Point> path; int i = 1; while(true){ char strp [3]; sprintf(strp,"p%d",i); ValueMap pointMap= group->objectNamed(strp); if(pointMap.empty()) break; path.push_back(Point(pointMap.at("x").asFloat(),pointMap.at("y").asFloat())); i ++; } Point point = t->getLocationInView()/32;//獲得屏幕的座標/32 point = Point((int)point.x,(int)point.y);//獲得地圖的座標 TMXLayer *role = map->layerNamed("role");//獲得地圖上的layer int gid = role->tileGIDAt(point);//獲得該地圖座標上的gid role->setTileGID(1,point);//設置該地圖座標上的GID,GID一旦改變地圖座標上的東西也改變