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,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" usingnamespaceCocosDenshion; 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", menu_selector(LayerStart::easy)); CcmenuItem *hardItem =CcmenuItemFont::create("Hard", menu_selector(LayerStart::hard)); CcmenuItem *veryHardItem =CcmenuItemFont::create("VeryHard", this,menu_selector(LayerStart::veryHard)); CcmenuItem *quitItem =CcmenuItemFont::create("Quit", 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,user 0 0 |
其它问题:略 |
总结
以上是内存溢出为你收集整理的1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结全部内容,希望文章能够帮你解决1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)