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 |
其它问题:略 |