转自:http://blog.csdn.net/swordfishx82/article/details/45418031
前几天想复习一下cocos2dx和c++,所以网上down了几段代码,不过win32下的工程比较难找,所以找了一些都是mac下的代码,上一篇2048就是其中一个,mac的项目移植到win32下,首先问题就是转码,不过转完码还是不能直接运行的,比如说在cocos2dx2.0时代,回调函数的参数问题,这个在3.0之后因为全新的根类ref出现回调麻烦减少了很多,不过好在源码也是c++的,改的难度也不算大,先看下最后的效果,代码比较完整,几乎游戏该有的部分都实现了,具体代码我就不讲了,后面会直接贴出来,我这么说说里面几个我受益的地方,首先是移植过程中遇到的一个问题,就是数组初始化
这个在mac下的数组定义在win32下就么有办法在vs2012下初始化了,我上网查了一下c++11的特性里面明明有这个的添加,mac下的ide直接对数组进行了初始化,但在vs2012下就不行,所以我们还是回到vs传统的数组初始化方式上我们在cpp中的init里面加入一个for循环来给int mapinfo[9][16];来赋值,其他需要注意的地方就不多了,需要的自己建工程看吧,以前也写过一个塔防游戏,之所以移植这个塔防是因为这个游戏里面使用了一些我认为不错的东西,首先用关卡文件来设定关卡,第一次使用关卡文件的读取方式,这里可以看下,
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEplistPUBLIC"-//Apple//DTDPLIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plistversion="1.0">
- <dict>
- <key>levelmap</key>
- <key>mapfile</key>
- <string>tilemap0.tmx</string>
- <key>tildfile</key>
- <string>tiles.png</string>
- <key>bgimg</key>
- <string>playbg.png</string>
- </dict>
- <key>levelinfo</key>
- <dict>
- <key>levelnum</key>
- <integer>1</integer>
- <key>money</key>
- <integer>500</integer>
- <key>npcgroup</key>
- <array>
- <array>
- <key>npc_HP</key>
- <integer>10</integer>
- <key>npc_Speed</key>
- <string>0.5</string>
- <key>npc_Price</key>
- <string>100</string>
- <key>npc_Type</key>
- <string>1</string>
- </dict>
- </array>
- <string>2</string>
- <integer>30</integer>
- <integer>40</integer>
- <string>3</string>
- </array>
- </plist>
第一关的关卡文件,读取方式我们看下代码
- ValueMaplevelInfo=FileUtils::getInstance()->getValueMapFromFile(StringUtils::format("level_00%d.plist",nowLevel));
- //获取背景地图
- std::stringbg=levelInfo["levelmap"].asValueMap()["bgimg"].asString();
- autosp=Sprite::create(bg);
- sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,(Director::getInstance()->getWinSize().height/2)));
- this->addChild(sp);
-
- //添加地图
- std::stringmapf=levelInfo["levelmap"].asValueMap()["mapfile"].asString();
- automap=TMXTiledMap::create(mapf);
- this->addChild(map);
- map->setTag(888);
-
- initAllPoint(map);
- ////产生敌人
- this->schedule(schedule_selector(GameScene::newEnemy),3);
- //this->enemyCount=20;
- autospritetool=Sprite::createWithSpriteFrameName("toolbg.png");
- spritetool->setAnchorPoint(Point(0.5f,1));
- spritetool->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,Director::getInstance()->getWinSize().height));
- this->addChild(spritetool);
- spritetool->setTag(2000);
- //初始化钱
- intstartmoney=levelInfo["levelinfo"].asValueMap()["money"].asInt();
- this->money=startmoney;
- automoneyLabel=Label::createWithBMFont("fonts/bitmapFontChinese.fnt","");
- moneyLabel->setPosition(Vec2(spritetool->getContentSize().width/8,spritetool->getContentSize().height/2));
- moneyLabel->setAnchorPoint(Point(0,0.5f));
- moneyLabel->setTag(2002);
- automoneyText=std::to_string(money);
- moneyLabel->setString(moneyText);
- spritetool->addChild(moneyLabel);
- //当前关卡一共给多少波,每波的数量
- this->levelAllNpc=levelInfo["levelinfo"].asValueMap()["npcgroup"].asValueVector();
copy
//读取关卡信息中的数据,获取当前波数,当前怪物
inttype=0;
inthp=0;
if(npcNumber_index<npcNumberCount){
type=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Type"].asInt();
CCLOG("newEnemytype=%d",type);
hp=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_HP"].asInt();
CCLOG("newEnemyhp=%d",hp);
copy
<prename="code"class="cpp">price=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Price"].asInt();
speed=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Speed"].asInt();
看一下.h中的定义
copy
intnowLevel;
intnpcGroupCount;
intnpcNumberCount;
voidinitLevel();
intnpcGroup_index;
intnpcNumber_index;
ValueVectorlevelAllNpc;
使用这种方法大大的简化了程序的工作量,真的很不错,另外,开始菜单使用了
copy
#include"MenuScene.h"
#include"GameScene.h"
Scene*MenuScene::createScene(){
autoscene=Scene::create();
autolayer=MenuScene::create();
scene->addChild(layer);
returnscene;
}
boolMenuScene::init(){
if(!Layer::init()){
returnfalse;
//加载ui编辑器编辑好的ui
autonode=GUIReader::getInstance()->widgetFromJsonFile("NewUi_1/NewUi_1.json");
this->addChild(node);
Button*btStart=(Button*)node->getChildByTag(7);
btStart->addTouchEventListener(CC_CALLBACK_2(MenuScene::onclick,this));
returntrue;
voidMenuScene::onclick(Ref*obj,Widget::TouchEventTypet){
switch(t)
{
caseWidget::TouchEventType::ENDED:
autoscene=GameScene::createScene();
Director::getInstance()->replaceScene(TransitionFlipX::create(0.5f,scene));}
break;
default:
}
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|