前几天想复习一下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];来赋值,其他需要注意的地方就不多了,需要的自己建工程看吧,以前也写过一个塔防游戏,之所以移植这个塔防是因为这个游戏里面使用了一些我认为不错的东西,首先用关卡文件来设定关卡,第一次使用关卡文件的读取方式,这里可以看下,
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>levelmap</key> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <array> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <dict> <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> <array> <dict> <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>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <array> <dict> <key>npc_HP</key> <integer>40</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>3</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <key>npc_HP</key> <integer>40</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>3</string> </dict> <dict> <key>npc_HP</key> <integer>30</integer> <key>npc_Speed</key> <string>0.5</string> <key>npc_Price</key> <string>100</string> <key>npc_Type</key> <string>2</string> </dict> <dict> <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> </array> </dict> </dict> </plist>第一关的关卡文件,读取方式我们看下代码
//添加背景 ValueMap levelInfo=FileUtils::getInstance()->getValueMapFromFile(StringUtils::format("level_00%d.plist",nowLevel)); //获取背景地图 std::string bg=levelInfo["levelmap"].asValueMap()["bgimg"].asString(); auto sp=Sprite::create(bg); sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,(Director::getInstance()->getWinSize().height/2))); this->addChild(sp); //添加地图 std::string mapf=levelInfo["levelmap"].asValueMap()["mapfile"].asString(); auto map=TMXTiledMap::create(mapf); this->addChild(map); map->setTag(888); //加载所有点 initAllPoint(map); ////产生敌人 this->schedule(schedule_selector(GameScene::newEnemy),3); //this->enemyCount=20; auto spritetool=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); //初始化钱 int startmoney=levelInfo["levelinfo"].asValueMap()["money"].asInt(); this->money=startmoney;//金币 auto moneyLabel=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); auto moneyText=std::to_string(money); moneyLabel->setString(moneyText); spritetool->addChild(moneyLabel); //当前关卡一共给多少波,每波的数量 this->levelAllNpc=levelInfo["levelinfo"].asValueMap()["npcgroup"].asValueVector();//怪物总共波数
调用上面读取的文件数据
//读取关卡信息中的数据,获取当前波数,当前怪物 int type=0; int hp=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);
<pre name="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中的定义
int nowLevel;//当前关卡编号 int npcGroupCount;//当前关共有多少波怪物 int npcNumberCount;//当前波共有多少怪物 void initLevel();//初始化当前关卡 int npcGroup_index;//当前第几波 int npcNumber_index;//当前第几个 ValueVector levelAllNpc;//当前关卡的所有怪物定义使用这种方法大大的简化了程序的工作量,真的很不错,另外,开始菜单使用了
#include "MenuScene.h" #include"GameScene.h" Scene * MenuScene::createScene(){ auto scene=Scene::create(); auto layer=MenuScene::create(); scene->addChild(layer); return scene; } bool MenuScene:: init(){ if(!Layer::init()){ return false; } //加载ui编辑器编辑好的ui auto node= GUIReader::getInstance()->widgetFromJsonFile("NewUi_1/NewUi_1.json");//从组件中得到一个josn this->addChild(node); Button*btStart=(Button*)node->getChildByTag(7); btStart->addTouchEventListener(CC_CALLBACK_2(MenuScene::onclick,this)); return true; } void MenuScene:: onclick(Ref* obj,Widget::TouchEventType t){ switch (t) { case Widget::TouchEventType::ENDED: { auto scene=GameScene::createScene(); Director::getInstance()->replaceScene(TransitionFlipX::create(0.5f,scene));} break; default: break; } }
cocos stdio1.6来编辑,上面是调用代码,其他方面的代码跟正常情况区别不是很大,我这里直接贴出来就行了, 代码下载点这里 原文链接:https://www.f2er.com/cocos2dx/343183.html