1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结-阿里云开发者社区

开发者社区> 涂作权> 正文

1.cocos2dx记忆卡片游戏代码、并将游戏移植到“华为荣耀”手机上、移植中的问题总结

简介:  1记忆卡片游戏代码 CardItem.h #pragmaonce #ifndef__CardItem_H__ #define__CardItem_H__   #include"cocos2d.h" USING_NS_CC;   classCardItem :publicCCSprite {
+关注继续查看


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看到如下配置:

修改Androidmakefile

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模拟器进行运行,cocos2dxAndroid模拟器上运行由问题)。

右击game项目Android项目进行run(这里以华为荣耀作为测试机)。

运行效果:

从下面game即使移植到华为荣耀手机上的cocos2d-x记忆卡片游戏


打开bin,拷贝bak文件即可用:



7 横屏、竖屏设置

androidAndroidManifest.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

其它问题:略

 



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Java基础-18总结Map,HashMap,HashMap与Hashtable区别,Collections工具类
你需要的是什么,直接评论留言。 获取更多资源加微信公众号“Java帮帮” (是公众号,不是微信好友哦) 还有“Java帮帮”今日头条号,技术文章与新闻,每日更新,欢迎阅读 学习交流请加Java帮帮交流QQ群553841695 分享是一种美德,分享更快乐! 1:Map(掌握) (1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只
2450 0
Java调用WeChat's API总结
  微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务;而后者仅仅能够浏览页面,通过用户的输入信息与用户互动。   本人根据个人的经历总结从小白到入门的过程,是一种记录,也是对渴望入门者的一种指引。
1389 0
1.将cocos2d-x项目移植到Linux环境下,将cocos2d-x项目移植到手机上
 1.将cocos2d-x相关的软件包拷贝到Linux下。 2对上面的安装包进行解压 unzipcocos2d-x-2.2.3.zip unzipcocos2d-x-3.2.zip 3执行cocos2dx-2.2.3/install-deps-linux.sh(执行这个命令需要有网络) 4安装QT,命令:sudo
1342 0
+关注
涂作权
java,架构,编程语言相关专家
1234
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载