1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结
1記憶卡片游戲代碼
| CardItem.h |
| #pragmaonce #ifndef__CardItem_H__ #define__CardItem_H__ ? #include"cocos2d.h" USING_NS_CC; ? classCardItem :publicCCSprite { public: ???staticCardItem *create(intidx); ???boolinit(intidx); ? ???CCLabelTTF *ttf; ???CCSprite *bg; ???CC_SYNTHESIZE(int,_index,Idx); ? ???voidshowWhite(); ???voidshowText(); }; ? #endif |
| CardItem.cpp |
| #include"CardItem.h" ? CardItem *CardItem::create(intidx) { ???CardItem *pRet =newCardItem; ???if (pRet &&pRet->init(idx)) ???{ ???????pRet->autorelease(); ???} ???else ???{ ???????deletepRet; ???????pRet =NULL; ???} ???returnpRet; } ? boolCardItem::init(intidx) { ???CCSprite::init(); ???setIdx(idx); ? ???setContentSize(CCSizeMake(80,80)); ???setAnchorPoint(ccp(0,0)); ? ???//設置字體 ???CCString *str =CCString::createWithFormat("%d",idx); ???ttf =CCLabelTTF::create(str->getCString(),"Courier New", 50); ???ttf->setPosition(ccp(40, 40)); ???this->addChild(ttf); ? ???bg =CCSprite::create(); ???bg->setTextureRect(CCRectMake(0,0,77,77)); ???bg->setAnchorPoint(ccp(0,0)); ???bg->setColor(ccc3(255,255,255)); ???this->addChild(bg); ? ???returntrue; } ? voidCardItem::showWhite() { ???ttf->setVisible(false); ???bg->setVisible(true); } ? voidCardItem::showText() { ???ttf->setVisible(true); ???bg->setVisible(false); } |
| LayerGame.h |
| #ifndef__LayerGame_H__ #define__LayerGame_H__ ? #include"cocos2d.h" #include"vector" usingnamespacestd; USING_NS_CC; ? classLayerGame :publicCCLayer { public: ???staticCCScene *scene(intdegree); ???staticLayerGame *create(intdegree); ???boolinit(intdegree); ? ???voidgetAllPoint(); ???voidgetAvailablePoint(); ???voidaddCard(); ???voidshowCardAllWhite(); ? ???CCPointArray *allPoints; ???CCArray?????*allCards; ???vector<int>vec; ? ???CC_SYNTHESIZE(int,_degere, Degree); ???CC_SYNTHESIZE(int,_clickStart, ClickStart); ? ???virtualboolccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); }; ? #endif |
| LayerGame.cpp |
| #include"LayerGame.h" #include"CardItem.h" #include"stdlib.h" #include"time.h" #include"LayerOver.h" #include"cocos-ext.h"?//支持聲音的 #include"SimpleAudioEngine.h" usingnamespace?CocosDenshion; USING_NS_CC_EXT;???????//放開擴展庫 ? CCScene *LayerGame::scene(intdegree) { ???CCScene *scene =CCScene::create(); ???LayerGame *layer =LayerGame::create(degree); ???scene->addChild(layer); ???returnscene; } ? LayerGame *LayerGame::create(intdegree) { ???LayerGame *pRet =newLayerGame; ???if (pRet &&pRet->init(degree)) ???{ ???????pRet->autorelease(); ???} ???else ???{ ???????deletepRet; ???????pRet =NULL; ???} ???returnpRet; } ? boolLayerGame::init(intdegree) { ???CCLayer::init(); ? ???setDegree(degree); ???setClickStart(0); ???getAllPoint(); ???getAvailablePoint(); ???addCard(); ? ???setTouchEnabled(true); ???setTouchMode(kCCTouchesOneByOne); ? ???SimpleAudioEngine::sharedEngine()->preloadEffect("boom.mp3"); ? ???returntrue; } ? voidLayerGame::getAllPoint() { ???allPoints =CCPointArray::create(60); ???allPoints->retain(); ???for (inti = 0;i < 10;i++) ???{ ???????for (intj = 0;j < 6;j++) ???????{ ???????????allPoints->addControlPoint(ccp(80 * i,80 *j)); ???????} ???} } ? //獲得要作為游戲點的point voidLayerGame::getAvailablePoint() { ???srand(time(NULL)); ???while (1) ???{ ???????intidx =rand() % 60; ???????vector<int>::iteratoritr =vec.begin(); ???????for (;itr !=vec.end(); ++itr) ???????{ ???????????if (*itr ==idx) ???????????????break; ???????} ???????if (itr ==vec.end()) ???????{ ???????????vec.push_back(idx); ???????????if (vec.size() == getDegree()) ???????????????break; ???????} ???} } ? voidLayerGame::addCard() { ???allCards =CCArray::create(); ???allCards->retain(); ???for (inti = 0;i <getDegree();i++) ???{ ???????CardItem *item =CardItem::create(i); ???????item->setPosition((CCPoint)allPoints->getControlPointAtIndex(vec[i])); ???????addChild(item); ???????item->showText(); ???????allCards->addObject(item); ???} } ? voidLayerGame::showCardAllWhite() { ???CCObject *obj; ???CCARRAY_FOREACH(allCards,obj) ???{ ???????CardItem *spr = (CardItem *)obj; ???????spr->showWhite(); ???} } ? boolLayerGame::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent) { ???for (inti = 0;i <allCards->count();i++) ???{ ???????//判斷是否點中 ???????if (((CardItem*)allCards->objectAtIndex(i))->boundingBox(). ???????????containsPoint(pTouch->getLocation())) ???????{ ???????????showCardAllWhite(); ???????????SimpleAudioEngine::sharedEngine()->playEffect("boom.mp3"); ???????????if (getClickStart() == ((CardItem*)allCards->objectAtIndex(i))->getIdx()) ???????????{ ??????????????((CardItem*)allCards->objectAtIndex(i))->removeFromParentAndCleanup(true); ???????????????allCards->removeObjectAtIndex(i); ???????????????if (allCards->count() == 0) ???????????????{ ???????????????????CCScene *scene =LayerOver::scene(true); ???????????????????CCDirector::sharedDirector()->replaceScene(scene); ???????????????????CCLog("you win game"); ???????????????} ???????????} ???????????else ???????????{ ???????????????CCScene *scene =LayerOver::scene(false); ???????????????CCDirector::sharedDirector()->replaceScene(scene); ???????????????CCLog("you lose game"); ???????????} ???????????_clickStart++; ???????} ???} ???returntrue; } |
| LayerOver.h |
| #ifndef__LayerOver_H__ #define__LayerOver_H__ ? ? #include"cocos2d.h" USING_NS_CC; ? ? classLayerOver :publicCCLayer { public: ???staticCCScene *scene(boolres); ???staticLayerOver?*create(boolres); ???boolinit(boolres); ? ???voidmenuCallBack(CCObject * obj); }; ? #endif |
| LayerOver.cpp |
| #include"LayerOver.h" #include"LayerStart.h" ? ? CCScene *LayerOver::scene(boolres) { ???CCScene *scene =CCScene::create(); ???LayerOver *layer =LayerOver::create(res); ???scene->addChild(layer); ???returnscene; } LayerOver?*LayerOver::create(boolres) { ???LayerOver *pRet =newLayerOver(); ???if (pRet &&pRet->init(res)) ???{ ???????pRet->autorelease(); ???} ???else ???{ ???????deletepRet; ???????pRet =NULL; ???} ???returnpRet; } boolLayerOver::init(boolres) { ???CCLayer::init(); ? ???CCMenu *menu =CCMenu::create(); ???CCMenuItem *back =CCMenuItemFont::create("Back",this,menu_selector(LayerOver::menuCallBack)); ? ???if (res ==true) ???{ ???????CCMenuItem *winItem =CCMenuItemFont::create("You Win"); ???????menu->addChild(winItem); ???????menu->addChild(back); ???} ???else ???{ ???????CCMenuItem *failItem =CCMenuItemFont::create("You Failed"); ???????menu->addChild(failItem); ???????menu->addChild(back); ???} ? ???addChild(menu); ???menu->alignItemsVerticallyWithPadding(30); ? ? ???returntrue; } ? ? voidLayerOver::menuCallBack(CCObject *obj) { ???CCScene *scene =LayerStart::scene(); ???CCDirector::sharedDirector()->replaceScene(scene); } |
| LayerStart.h |
| #ifndef__LayerStart_H__ #define__LayerStart_H__ ? #include"cocos2d.h" USING_NS_CC; classLayerStart :publicCCLayer { public: ???CREATE_FUNC(LayerStart); ???staticCCScene *scene(); ???boolinit(); ? ???voideasy(CCObject * obj); ???voidhard(CCObject * obj); ???voidveryHard(CCObject * obj); ???voidquit(CCObject * obj); ? ???enumDEGREE ???{ ???????EASY = 5,HARD = 7,VERYHARD = 9 ???}; }; ? #endif |
| LayerStart.cpp |
| #include"LayerStart.h" #include"LayerGame.h" ? CCScene *LayerStart::scene() { ???CCScene *scene =CCScene::create(); ???LayerStart *layer =LayerStart::create(); ???scene->addChild(layer); ???returnscene; } ? boolLayerStart::init() { ???CCLayer::init(); ???CCMenuItem *easyItem =CCMenuItemFont::create("Easy",this, ???????menu_selector(LayerStart::easy)); ???CCMenuItem *hardItem =CCMenuItemFont::create("Hard",this, ???????menu_selector(LayerStart::hard)); ???CCMenuItem *veryHardItem =CCMenuItemFont::create("VeryHard", ???????this,menu_selector(LayerStart::veryHard)); ???CCMenuItem *quitItem =CCMenuItemFont::create("Quit",this, ???????menu_selector(LayerStart::quit)); ? ???CCMenu *menu =CCMenu::create(easyItem,hardItem,veryHardItem,quitItem,NULL); ? ???menu->alignItemsVerticallyWithPadding(30); ???addChild(menu); ? ???returntrue; } ? voidLayerStart::easy(CCObject * obj) { ???CCScene *scene =LayerGame::scene(EASY); ???CCDirector::sharedDirector()->replaceScene(scene); } ? voidLayerStart::hard(CCObject * obj) { ???CCScene *scene =LayerGame::scene(HARD); ???CCDirector::sharedDirector()->replaceScene(scene); } ? voidLayerStart::veryHard(CCObject * obj) { ???CCScene *scene =LayerGame::scene(VERYHARD); ???CCDirector::sharedDirector()->replaceScene(scene); } ? voidLayerStart::quit(CCObject * obj) { ???exit(1); } |
| 運行結果:
點擊進入Easy后的效果:
失敗后的效果:
|
2記憶卡片手機移植
| 環境準備:所需軟件: 32位:
64位
|
| 軟件安裝(注意不要有中文目錄): 1.??????安裝JDK jdk-7u25-windows-i586.exe 2.??????安裝安卓SDK
3.??????安裝安裝NDK
4.??????打開(F:\TransPlant\adt-bundle-windows-x86-20140321\eclipse\eclipse.exe):
效果圖如下: 將game游戲項目導入,導入錄入路徑:E:\Installed\cocos2d-x-2.2.3\projects\game\proj.android 點擊:File -- >Import
運行結果:
?
|
| 5安裝cygin,從官網(http://cygwin.com/install.html)上下載cygwin.
下載后軟件: , A雙擊上面的64位或者32位的軟件進行安裝。這里選擇64位進行安裝。
配置cygwin. A修改:cygwin64\etc中的fstab,將最后一行注釋掉,而換成如下的配置: none /cygdrive cygdrive binary,noacl,posix=0,user 0 0
? 左面cygwin的運行效果圖如下:
|
| 6配置環境變量 將Cygwin加載系統路徑到Eclipse本地路徑中。解決bash找不到的問題,打開Eclipse->windows-preference看到如下配置:
|
| 7?修改Android的makefile
|
| 8配置項目中的build_native.sh文件,加上: chmod 777 "$APP_ANDROID_ROOT"/assets -R
|
| 9導入cocos2dx的另外一個Android應用,只有這樣才能讓本項目運行起來 加載路徑是:E:\Installed\cocos2d-x-2.2.3\cocos2dx\platform\android\java
點擊確定。導入后的截圖:
? |
| 8?將游戲移植到手機,將手機USB線接上電腦(注意,不要使用Android模擬器進行運行,cocos2dx在Android模擬器上運行由問題)。 右擊game項目以Android項目進行run(這里以華為榮耀作為測試機)。
運行效果:
從下面game即使移植到華為榮耀手機上的cocos2d-x記憶卡片游戲
打開bin,拷貝bak文件即可用:
|
| 7?橫屏、豎屏設置 android的AndroidManifest.xml文件中: screenOrientation="landscape"為橫屏, screenOrientation="portrait"為豎屏 |
?
移植錯誤總結:
| [2014-10-23 17:11:41 - myhello] Unable to resolve target 'android-8' [2014-10-23 17:24:58 - libcocos2dx] Unable to resolve target 'android-8' [2014-10-23 17:33:15 - SDK Manager] Created AVD 'AVD19' based on Android 4.4.2, ARM (armeabi-v7a) processor, [2014-10-23 17:33:15 - SDK Manager] with the following hardware config: [2014-10-23 17:33:15 - SDK Manager] disk.dataPartition.size=200M [2014-10-23 17:33:15 - SDK Manager] hw.accelerometer=yes [2014-10-23 17:33:15 - SDK Manager] hw.audioInput=yes [2014-10-23 17:33:15 - SDK Manager] hw.battery=yes [2014-10-23 17:33:15 - SDK Manager] hw.camera.back=none [2014-10-23 17:33:15 - SDK Manager] hw.dPad=no [2014-10-23 17:33:15 - SDK Manager] hw.device.hash2=MD5:b1157894a81a9869f18a16ee4591a709 [2014-10-23 17:33:15 - SDK Manager] hw.device.manufacturer=Generic [2014-10-23 17:33:15 - SDK Manager] hw.device.name=3.2in HVGA slider (ADP1) [2014-10-23 17:33:15 - SDK Manager] hw.gps=yes [2014-10-23 17:33:15 - SDK Manager] hw.keyboard=yes [2014-10-23 17:33:15 - SDK Manager] hw.keyboard.lid=yes [2014-10-23 17:33:15 - SDK Manager] hw.lcd.density=160 [2014-10-23 17:33:15 - SDK Manager] hw.mainKeys=yes [2014-10-23 17:33:15 - SDK Manager] hw.ramSize=512 [2014-10-23 17:33:15 - SDK Manager] hw.sdCard=yes [2014-10-23 17:33:15 - SDK Manager] hw.sensors.orientation=yes [2014-10-23 17:33:15 - SDK Manager] hw.sensors.proximity=yes [2014-10-23 17:33:15 - SDK Manager] hw.trackBall=no [2014-10-23 17:33:15 - SDK Manager] skin.dynamic=no [2014-10-23 17:33:15 - SDK Manager] vm.heapSize=16 [2014-10-23 17:35:56 - myhello]????(skipping file '.gitignore' due to ANDROID_AAPT_IGNORE pattern '.*') [2014-10-23 17:35:56 - myhello]??????Unable to add 'F:\cocos2d-x-2.2.3\cocos2d-x-2.2.3\projects\myhello\proj.android\assets\CloseNormal.png': Zip add failed [2014-10-23 17:35:56 - myhello] ERROR: unable to process assets while packaging 'F:\cocos2d-x-2.2.3\cocos2d-x-2.2.3\projects\myhello\proj.android\bin\resources.ap_' [2014-10-23 17:35:56 - myhello] ERROR: packaging of 'F:\cocos2d-x-2.2.3\cocos2d-x-2.2.3\projects\myhello\proj.android\bin\resources.ap_' failed ? |
| 上面的錯誤原因是: 要修改:cygwin64\etc中的fstab,將最后一行注釋掉,而換成如下的配置: none /cygdrive cygdrive binary,noacl,posix=0,user 0 0 |
| 其它問題:略 |
?
總結
以上是生活随笔為你收集整理的1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应援打榜是什么意思 其实就是粉丝集资给偶
- 下一篇: 蚂蚁借呗会影响房贷吗