当前位置 : 主页 > 手机开发 > 其它 >

cocos3.5塔防游戏开发

来源:互联网 收集:自由互联 发布时间:2021-06-13
转自:http://blog.csdn.net/swordfishx82/article/details/45418031 前几天想复习一下cocos2dx和c++,所以网上down了几段代码,不过win32下的工程比较难找,所以找了一些都是mac下的代码,上一篇2048就是

转自: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];来赋值,其他需要注意的地方就不多了,需要的自己建工程看吧,以前也写过一个塔防游戏,之所以移植这个塔防是因为这个游戏里面使用了一些我认为不错的东西,首先用关卡文件来设定关卡,第一次使用关卡文件的读取方式,这里可以看下,

[html]  view plain  copy  print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <dict>  
  5.     <key>levelmap</key>  
  6.     <dict>  
  7.         <key>mapfile</key>  
  8.         <string>tilemap0.tmx</string>  
  9.         <key>tildfile</key>  
  10.         <string>tiles.png</string>  
  11.         <key>bgimg</key>  
  12.         <string>playbg.png</string>  
  13.     </dict>  
  14.     <key>levelinfo</key>  
  15.     <dict>  
  16.         <key>levelnum</key>  
  17.         <integer>1</integer>  
  18.         <key>money</key>  
  19.         <integer>500</integer>  
  20.         <key>npcgroup</key>  
  21.         <array>  
  22.             <array>  
  23.                 <dict>  
  24.                     <key>npc_HP</key>  
  25.                     <integer>10</integer>  
  26.                     <key>npc_Speed</key>  
  27.                     <string>0.5</string>  
  28.                     <key>npc_Price</key>  
  29.                     <string>100</string>  
  30.                     <key>npc_Type</key>  
  31.                     <string>1</string>  
  32.                 </dict>  
  33.                 <dict>  
  34.                     <key>npc_HP</key>  
  35.                     <integer>10</integer>  
  36.                     <key>npc_Speed</key>  
  37.                     <string>0.5</string>  
  38.                     <key>npc_Price</key>  
  39.                     <string>100</string>  
  40.                     <key>npc_Type</key>  
  41.                     <string>1</string>  
  42.                 </dict>  
  43.                 <dict>  
  44.                     <key>npc_HP</key>  
  45.                     <integer>10</integer>  
  46.                     <key>npc_Speed</key>  
  47.                     <string>0.5</string>  
  48.                     <key>npc_Price</key>  
  49.                     <string>100</string>  
  50.                     <key>npc_Type</key>  
  51.                     <string>1</string>  
  52.                 </dict>  
  53.                 <dict>  
  54.                     <key>npc_HP</key>  
  55.                     <integer>10</integer>  
  56.                     <key>npc_Speed</key>  
  57.                     <string>0.5</string>  
  58.                     <key>npc_Price</key>  
  59.                     <string>100</string>  
  60.                     <key>npc_Type</key>  
  61.                     <string>1</string>  
  62.                 </dict>  
  63.                 <dict>  
  64.                     <key>npc_HP</key>  
  65.                     <integer>10</integer>  
  66.                     <key>npc_Speed</key>  
  67.                     <string>0.5</string>  
  68.                     <key>npc_Price</key>  
  69.                     <string>100</string>  
  70.                     <key>npc_Type</key>  
  71.                     <string>1</string>  
  72.                 </dict>  
  73.                 <dict>  
  74.                     <key>npc_HP</key>  
  75.                     <integer>10</integer>  
  76.                     <key>npc_Speed</key>  
  77.                     <string>0.5</string>  
  78.                     <key>npc_Price</key>  
  79.                     <string>100</string>  
  80.                     <key>npc_Type</key>  
  81.                     <string>1</string>  
  82.                 </dict>  
  83.                 <dict>  
  84.                     <key>npc_HP</key>  
  85.                     <integer>10</integer>  
  86.                     <key>npc_Speed</key>  
  87.                     <string>0.5</string>  
  88.                     <key>npc_Price</key>  
  89.                     <string>100</string>  
  90.                     <key>npc_Type</key>  
  91.                     <string>1</string>  
  92.                 </dict>  
  93.                 <dict>  
  94.                     <key>npc_HP</key>  
  95.                     <integer>10</integer>  
  96.                     <key>npc_Speed</key>  
  97.                     <string>0.5</string>  
  98.                     <key>npc_Price</key>  
  99.                     <string>100</string>  
  100.                     <key>npc_Type</key>  
  101.                     <string>1</string>  
  102.                 </dict>  
  103.                 <dict>  
  104.                     <key>npc_HP</key>  
  105.                     <integer>10</integer>  
  106.                     <key>npc_Speed</key>  
  107.                     <string>0.5</string>  
  108.                     <key>npc_Price</key>  
  109.                     <string>100</string>  
  110.                     <key>npc_Type</key>  
  111.                     <string>1</string>  
  112.                 </dict>  
  113.                 <dict>  
  114.                     <key>npc_HP</key>  
  115.                     <integer>10</integer>  
  116.                     <key>npc_Speed</key>  
  117.                     <string>0.5</string>  
  118.                     <key>npc_Price</key>  
  119.                     <string>100</string>  
  120.                     <key>npc_Type</key>  
  121.                     <string>1</string>  
  122.                 </dict>  
  123.             </array>  
  124.             <array>  
  125.                 <dict>  
  126.                     <key>npc_HP</key>  
  127.                     <integer>10</integer>  
  128.                     <key>npc_Speed</key>  
  129.                     <string>0.5</string>  
  130.                     <key>npc_Price</key>  
  131.                     <string>100</string>  
  132.                     <key>npc_Type</key>  
  133.                     <string>1</string>  
  134.                 </dict>  
  135.                 <dict>  
  136.                     <key>npc_HP</key>  
  137.                     <integer>10</integer>  
  138.                     <key>npc_Speed</key>  
  139.                     <string>0.5</string>  
  140.                     <key>npc_Price</key>  
  141.                     <string>100</string>  
  142.                     <key>npc_Type</key>  
  143.                     <string>1</string>  
  144.                 </dict>  
  145.                 <dict>  
  146.                     <key>npc_HP</key>  
  147.                     <integer>10</integer>  
  148.                     <key>npc_Speed</key>  
  149.                     <string>0.5</string>  
  150.                     <key>npc_Price</key>  
  151.                     <string>100</string>  
  152.                     <key>npc_Type</key>  
  153.                     <string>1</string>  
  154.                 </dict>  
  155.                 <dict>  
  156.                     <key>npc_HP</key>  
  157.                     <integer>10</integer>  
  158.                     <key>npc_Speed</key>  
  159.                     <string>0.5</string>  
  160.                     <key>npc_Price</key>  
  161.                     <string>100</string>  
  162.                     <key>npc_Type</key>  
  163.                     <string>1</string>  
  164.                 </dict>  
  165.                 <dict>  
  166.                     <key>npc_HP</key>  
  167.                     <integer>10</integer>  
  168.                     <key>npc_Speed</key>  
  169.                     <string>0.5</string>  
  170.                     <key>npc_Price</key>  
  171.                     <string>100</string>  
  172.                     <key>npc_Type</key>  
  173.                     <string>1</string>  
  174.                 </dict>  
  175.                 <dict>  
  176.                     <key>npc_HP</key>  
  177.                     <integer>10</integer>  
  178.                     <key>npc_Speed</key>  
  179.                     <string>0.5</string>  
  180.                     <key>npc_Price</key>  
  181.                     <string>100</string>  
  182.                     <key>npc_Type</key>  
  183.                     <string>1</string>  
  184.                 </dict>  
  185.                 <dict>  
  186.                     <key>npc_HP</key>  
  187.                     <integer>10</integer>  
  188.                     <key>npc_Speed</key>  
  189.                     <string>0.5</string>  
  190.                     <key>npc_Price</key>  
  191.                     <string>100</string>  
  192.                     <key>npc_Type</key>  
  193.                     <string>1</string>  
  194.                 </dict>  
  195.                 <dict>  
  196.                     <key>npc_HP</key>  
  197.                     <integer>10</integer>  
  198.                     <key>npc_Speed</key>  
  199.                     <string>0.5</string>  
  200.                     <key>npc_Price</key>  
  201.                     <string>100</string>  
  202.                     <key>npc_Type</key>  
  203.                     <string>1</string>  
  204.                 </dict>  
  205.                 <dict>  
  206.                     <key>npc_HP</key>  
  207.                     <integer>10</integer>  
  208.                     <key>npc_Speed</key>  
  209.                     <string>0.5</string>  
  210.                     <key>npc_Price</key>  
  211.                     <string>100</string>  
  212.                     <key>npc_Type</key>  
  213.                     <string>1</string>  
  214.                 </dict>  
  215.                 <dict>  
  216.                     <key>npc_HP</key>  
  217.                     <integer>10</integer>  
  218.                     <key>npc_Speed</key>  
  219.                     <string>0.5</string>  
  220.                     <key>npc_Price</key>  
  221.                     <string>100</string>  
  222.                     <key>npc_Type</key>  
  223.                     <string>1</string>  
  224.                 </dict>  
  225.             </array>  
  226.             <array>  
  227.                 <dict>  
  228.                     <key>npc_HP</key>  
  229.                     <integer>10</integer>  
  230.                     <key>npc_Speed</key>  
  231.                     <string>0.5</string>  
  232.                     <key>npc_Price</key>  
  233.                     <string>100</string>  
  234.                     <key>npc_Type</key>  
  235.                     <string>2</string>  
  236.                 </dict>  
  237.                 <dict>  
  238.                     <key>npc_HP</key>  
  239.                     <integer>10</integer>  
  240.                     <key>npc_Speed</key>  
  241.                     <string>0.5</string>  
  242.                     <key>npc_Price</key>  
  243.                     <string>100</string>  
  244.                     <key>npc_Type</key>  
  245.                     <string>1</string>  
  246.                 </dict>  
  247.                 <dict>  
  248.                     <key>npc_HP</key>  
  249.                     <integer>30</integer>  
  250.                     <key>npc_Speed</key>  
  251.                     <string>0.5</string>  
  252.                     <key>npc_Price</key>  
  253.                     <string>100</string>  
  254.                     <key>npc_Type</key>  
  255.                     <string>2</string>  
  256.                 </dict>  
  257.                 <dict>  
  258.                     <key>npc_HP</key>  
  259.                     <integer>10</integer>  
  260.                     <key>npc_Speed</key>  
  261.                     <string>0.5</string>  
  262.                     <key>npc_Price</key>  
  263.                     <string>100</string>  
  264.                     <key>npc_Type</key>  
  265.                     <string>1</string>  
  266.                 </dict>  
  267.                 <dict>  
  268.                     <key>npc_HP</key>  
  269.                     <integer>30</integer>  
  270.                     <key>npc_Speed</key>  
  271.                     <string>0.5</string>  
  272.                     <key>npc_Price</key>  
  273.                     <string>100</string>  
  274.                     <key>npc_Type</key>  
  275.                     <string>2</string>  
  276.                 </dict>  
  277.                 <dict>  
  278.                     <key>npc_HP</key>  
  279.                     <integer>10</integer>  
  280.                     <key>npc_Speed</key>  
  281.                     <string>0.5</string>  
  282.                     <key>npc_Price</key>  
  283.                     <string>100</string>  
  284.                     <key>npc_Type</key>  
  285.                     <string>1</string>  
  286.                 </dict>  
  287.                 <dict>  
  288.                     <key>npc_HP</key>  
  289.                     <integer>30</integer>  
  290.                     <key>npc_Speed</key>  
  291.                     <string>0.5</string>  
  292.                     <key>npc_Price</key>  
  293.                     <string>100</string>  
  294.                     <key>npc_Type</key>  
  295.                     <string>2</string>  
  296.                 </dict>  
  297.                 <dict>  
  298.                     <key>npc_HP</key>  
  299.                     <integer>10</integer>  
  300.                     <key>npc_Speed</key>  
  301.                     <string>0.5</string>  
  302.                     <key>npc_Price</key>  
  303.                     <string>100</string>  
  304.                     <key>npc_Type</key>  
  305.                     <string>1</string>  
  306.                 </dict>  
  307.                 <dict>  
  308.                     <key>npc_HP</key>  
  309.                     <integer>30</integer>  
  310.                     <key>npc_Speed</key>  
  311.                     <string>0.5</string>  
  312.                     <key>npc_Price</key>  
  313.                     <string>100</string>  
  314.                     <key>npc_Type</key>  
  315.                     <string>2</string>  
  316.                 </dict>  
  317.                 <dict>  
  318.                     <key>npc_HP</key>  
  319.                     <integer>10</integer>  
  320.                     <key>npc_Speed</key>  
  321.                     <string>0.5</string>  
  322.                     <key>npc_Price</key>  
  323.                     <string>100</string>  
  324.                     <key>npc_Type</key>  
  325.                     <string>1</string>  
  326.                 </dict>  
  327.             </array>  
  328.             <array>  
  329.                 <dict>  
  330.                     <key>npc_HP</key>  
  331.                     <integer>40</integer>  
  332.                     <key>npc_Speed</key>  
  333.                     <string>0.5</string>  
  334.                     <key>npc_Price</key>  
  335.                     <string>100</string>  
  336.                     <key>npc_Type</key>  
  337.                     <string>3</string>  
  338.                 </dict>  
  339.                 <dict>  
  340.                     <key>npc_HP</key>  
  341.                     <integer>10</integer>  
  342.                     <key>npc_Speed</key>  
  343.                     <string>0.5</string>  
  344.                     <key>npc_Price</key>  
  345.                     <string>100</string>  
  346.                     <key>npc_Type</key>  
  347.                     <string>1</string>  
  348.                 </dict>  
  349.                 <dict>  
  350.                     <key>npc_HP</key>  
  351.                     <integer>30</integer>  
  352.                     <key>npc_Speed</key>  
  353.                     <string>0.5</string>  
  354.                     <key>npc_Price</key>  
  355.                     <string>100</string>  
  356.                     <key>npc_Type</key>  
  357.                     <string>2</string>  
  358.                 </dict>  
  359.                 <dict>  
  360.                     <key>npc_HP</key>  
  361.                     <integer>10</integer>  
  362.                     <key>npc_Speed</key>  
  363.                     <string>0.5</string>  
  364.                     <key>npc_Price</key>  
  365.                     <string>100</string>  
  366.                     <key>npc_Type</key>  
  367.                     <string>1</string>  
  368.                 </dict>  
  369.                 <dict>  
  370.                     <key>npc_HP</key>  
  371.                     <integer>30</integer>  
  372.                     <key>npc_Speed</key>  
  373.                     <string>0.5</string>  
  374.                     <key>npc_Price</key>  
  375.                     <string>100</string>  
  376.                     <key>npc_Type</key>  
  377.                     <string>2</string>  
  378.                 </dict>  
  379.                 <dict>  
  380.                     <key>npc_HP</key>  
  381.                     <integer>10</integer>  
  382.                     <key>npc_Speed</key>  
  383.                     <string>0.5</string>  
  384.                     <key>npc_Price</key>  
  385.                     <string>100</string>  
  386.                     <key>npc_Type</key>  
  387.                     <string>1</string>  
  388.                 </dict>  
  389.                 <dict>  
  390.                     <key>npc_HP</key>  
  391.                     <integer>30</integer>  
  392.                     <key>npc_Speed</key>  
  393.                     <string>0.5</string>  
  394.                     <key>npc_Price</key>  
  395.                     <string>100</string>  
  396.                     <key>npc_Type</key>  
  397.                     <string>2</string>  
  398.                 </dict>  
  399.                 <dict>  
  400.                     <key>npc_HP</key>  
  401.                     <integer>40</integer>  
  402.                     <key>npc_Speed</key>  
  403.                     <string>0.5</string>  
  404.                     <key>npc_Price</key>  
  405.                     <string>100</string>  
  406.                     <key>npc_Type</key>  
  407.                     <string>3</string>  
  408.                 </dict>  
  409.                 <dict>  
  410.                     <key>npc_HP</key>  
  411.                     <integer>30</integer>  
  412.                     <key>npc_Speed</key>  
  413.                     <string>0.5</string>  
  414.                     <key>npc_Price</key>  
  415.                     <string>100</string>  
  416.                     <key>npc_Type</key>  
  417.                     <string>2</string>  
  418.                 </dict>  
  419.                 <dict>  
  420.                     <key>npc_HP</key>  
  421.                     <integer>10</integer>  
  422.                     <key>npc_Speed</key>  
  423.                     <string>0.5</string>  
  424.                     <key>npc_Price</key>  
  425.                     <string>100</string>  
  426.                     <key>npc_Type</key>  
  427.                     <string>1</string>  
  428.                 </dict>  
  429.             </array>  
  430.         </array>  
  431.     </dict>  
  432. </dict>  
  433. </plist>  
第一关的关卡文件,读取方式我们看下代码 [cpp]  view plain  copy  print ?
  1. //添加背景  
  2.   ValueMap levelInfo=FileUtils::getInstance()->getValueMapFromFile(StringUtils::format("level_00%d.plist",nowLevel));     
  3.   //获取背景地图  
  4.   std::string bg=levelInfo["levelmap"].asValueMap()["bgimg"].asString();  
  5.     auto sp=Sprite::create(bg);  
  6. sp->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,(Director::getInstance()->getWinSize().height/2)));  
  7. this->addChild(sp);  
  8.   
  9.  //添加地图  
  10.  std::string mapf=levelInfo["levelmap"].asValueMap()["mapfile"].asString();  
  11. auto map=TMXTiledMap::create(mapf);  
  12. this->addChild(map);  
  13. map->setTag(888);  
  14. //加载所有点  
  15. initAllPoint(map);  
  16. ////产生敌人  
  17. this->schedule(schedule_selector(GameScene::newEnemy),3);  
  18. //this->enemyCount=20;  
  19. auto spritetool=Sprite::createWithSpriteFrameName("toolbg.png");  
  20. spritetool->setAnchorPoint(Point(0.5f,1));  
  21. spritetool->setPosition(Vec2(Director::getInstance()->getWinSize().width/2,Director::getInstance()->getWinSize().height));  
  22. this->addChild(spritetool);  
  23. spritetool->setTag(2000);  
  24.   
  25. //初始化钱  
  26. int startmoney=levelInfo["levelinfo"].asValueMap()["money"].asInt();  
  27. this->money=startmoney;//金币  
  28. auto moneyLabel=Label::createWithBMFont("fonts/bitmapFontChinese.fnt"," ");  
  29. moneyLabel->setPosition(Vec2(spritetool->getContentSize().width/8,spritetool->getContentSize().height/2));  
  30. moneyLabel->setAnchorPoint(Point(0,0.5f));  
  31. moneyLabel->setTag(2002);  
  32. auto moneyText=std::to_string(money);  
  33. moneyLabel->setString(moneyText);  
  34. spritetool->addChild(moneyLabel);  
  35.     //当前关卡一共给多少波, 每波的数量  
  36. this->levelAllNpc=levelInfo["levelinfo"].asValueMap()["npcgroup"].asValueVector();//怪物总共波数  
[cpp]  view plain  copy  print ?
  1. 调用上面读取的文件数据  
[cpp]  view plain  copy  print ?
  1. //读取关卡信息中的数据,获取当前波数,当前怪物  
  2.      int type=0;  
  3.   int hp=0;  
  4.   if(npcNumber_index<npcNumberCount){  
  5.   type=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Type"].asInt();  
  6.   CCLOG("newEnemytype=%d",type);  
  7.     hp=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_HP"].asInt();  
  8.   CCLOG("newEnemyhp=%d",hp);  
[cpp]  view plain  copy  print ?
  1. <pre name="code" class="cpp">price=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Price"].asInt();  
  2. speed=levelAllNpc.at(this->npcGroup_index).asValueVector().at(npcNumber_index).asValueMap()["npc_Speed"].asInt();  


 

 
看一下.h中的定义 

[cpp]  view plain  copy  print ?
  1. int nowLevel;//当前关卡编号  
  2.     int npcGroupCount;//当前关共有多少波怪物  
  3.     int npcNumberCount;//当前波共有多少怪物  
  4.     void initLevel();//初始化当前关卡  
  5.     int npcGroup_index;//当前第几波  
  6.     int npcNumber_index;//当前第几个  
  7.     ValueVector levelAllNpc;//当前关卡的所有怪物定义  
使用这种方法大大的简化了程序的工作量,真的很不错,另外,开始菜单使用了 [cpp]  view plain  copy  print ?
  1. #include "MenuScene.h"  
  2. #include"GameScene.h"  
  3. Scene * MenuScene::createScene(){  
  4.     auto scene=Scene::create();  
  5.     auto layer=MenuScene::create();  
  6.     scene->addChild(layer);  
  7.     return scene;  
  8. }  
  9. bool MenuScene:: init(){  
  10.     if(!Layer::init()){  
  11.         return false;  
  12.     }  
  13.     //加载ui编辑器编辑好的ui  
  14.     auto node= GUIReader::getInstance()->widgetFromJsonFile("NewUi_1/NewUi_1.json");//从组件中得到一个josn  
  15.     this->addChild(node);  
  16.     Button*btStart=(Button*)node->getChildByTag(7);  
  17.     btStart->addTouchEventListener(CC_CALLBACK_2(MenuScene::onclick,this));  
  18.   
  19.     return true;  
  20. }  
  21. void MenuScene:: onclick(Ref* obj,Widget::TouchEventType t){  
  22.     switch (t)  
  23.     {  
  24.     case Widget::TouchEventType::ENDED:  
  25.         {  
  26.         auto scene=GameScene::createScene();  
  27.         Director::getInstance()->replaceScene(TransitionFlipX::create(0.5f,scene));}  
  28.         break;  
  29.     default:  
  30.         break;  
  31.     }  
  32.   
  33. }  
网友评论