1. #include "HelloWorld.h"
  2.  
  3. USING_NS_CC;
  4.  
  5. CCScene* MyHelloWorld::scene()
  6. {
  7. // 'scene' is an autorelease object
  8. CCScene *scene = CCScene::create();
  9.  
  10. // 'layer' is an autorelease object
  11. MyHelloWorld *layer = MyHelloWorld::create();
  12.  
  13. // add layer as a child to scene
  14. scene->addChild(layer);
  15.  
  16. // return the scene
  17. return scene;
  18. }
  19.  
  20. // on "init" you need to initialize your instance
  21. bool MyHelloWorld::init()
  22. {
  23. //////////////////////////////
  24. // 1. super init first
  25. if ( !CCLayer::init() )
  26. {
  27. return false;
  28. }
  29.  
  30. initdefalut();
  31.  
  32. initmap();
  33.  
  34. return true;
  35. }
  36.  
  37. void MyHelloWorld::menuCloseCallback(CCObject* pSender)
  38. {
  39. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  40. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  41. #else
  42. CCDirector::sharedDirector()->end();
  43. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  44. exit(0);
  45. #endif
  46. #endif
  47. }
  48.  
  49. void MyHelloWorld::initmap()
  50. {
  51. this->scheduleUpdate();
  52. map=CCTMXTiledMap::create("my.tmx");
  53. CCTMXObjectGroup* obj1= map->objectGroupNamed("object1");
  54. wall=map->layerNamed("wall");
  55. floor=map->layerNamed("floor");
  56. meta=map->layerNamed("meta");
  57. CCDictionary* dict=obj1->objectNamed("sprite");
  58. float x=dict->valueForKey("x")->floatValue();
  59. float y=dict->valueForKey("y")->floatValue();
  60. float width=dict->valueForKey("width")->floatValue();
  61. float height=dict->valueForKey("height")->floatValue();
  62. int i=dict->valueForKey("key")->intValue();
  63. sprite=CCSprite::create("Player.png");
  64. sprite->retain();
  65. sprite->setAnchorPoint(ccp(0,0));
  66. sprite->setPosition(ccp(x,y));
  67. this->addChild(map,10,1);
  68.  
  69. map->addChild(sprite,0);
  70. CCLOG("%d",map->getChildren()->count());
  71. CCLOG("%d",floor->getZOrder());CCLOG("%d",wall->getZOrder());
  72. CCLOG("%d",meta->getZOrder());
  73. CCLOG("%d",sprite->getZOrder());
  74. CCLOG("%f.%f,%d",x,y,i);
  75. CCLOG("%f.%f,%d",width,height,i);
  76.  
  77. }
  78.  
  79. void MyHelloWorld::initdefalut()
  80. {
  81. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  82. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  83.  
  84. /////////////////////////////
  85. // 2. add a menu item with "X" image, which is clicked to quit the program
  86. // you may modify it.
  87.  
  88. // add a "close" icon to exit the progress. it's an autorelease object
  89. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  90. "CloseNormal.png",
  91. "CloseSelected.png",
  92. this,
  93. menu_selector(MyHelloWorld::menuCloseCallback));
  94.  
  95. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
  96. origin.y + pCloseItem->getContentSize().height/2));
  97.  
  98. // create menu, it's an autorelease object
  99. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  100. pMenu->setPosition(CCPointZero);
  101. this->addChild(pMenu, 20);
  102.  
  103. /////////////////////////////
  104. // 3. add your codes below...
  105.  
  106. // add a label shows "Hello World"
  107. // create and initialize a label
  108.  
  109. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "", 24);
  110.  
  111. // position the label on the center of the screen
  112. pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
  113. origin.y + visibleSize.height - pLabel->getContentSize().height));
  114.  
  115. // add the label as a child to this layer
  116. this->addChild(pLabel, 1);
  117.  
  118. // add "HelloWorld" splash screen"
  119. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  120.  
  121. // position the sprite on the center of the screen
  122. pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  123.  
  124. // add the sprite as a child to this layer
  125. this->addChild(pSprite, 0);
  126. }
  127.  
  128. void MyHelloWorld::onEnter()
  129. {
  130. CCLayer::onEnter();
  131. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
  132. }
  133.  
  134. bool MyHelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
  135. {
  136. CCPoint pointtemp= pTouch->getLocation(); //返回的是相当屏的本地坐标
  137. CCPoint touchPoint= this->convertTouchToNodeSpace(pTouch); //返回的是相对layer
  138. CCLOG("pointtemp:%f,%f",pointtemp.x,pointtemp.y);
  139. CCLOG("touchPoint:%f,%f",touchPoint.x,touchPoint.y);
  140.  
  141. //
  142. ////图块的惟一标识
  143. //
  144. //CCPoint mapTieldPoint=getTileCoordinate(point);
  145. //CCLOG("mapTieldPoint:%f,%f",mapTieldPoint.x,mapTieldPoint.y);
  146. //CCSize contentSize=map->getContentSize();
  147. //CCLOG("contentSize:%f,%f",contentSize.width,contentSize.height);
  148. //unsigned int tieldGid=floor->tileGIDAt(mapTieldPoint);
  149. //if(tieldGid!=0){
  150. // CCDictionary* dict=wall->propertiesForGID(tieldGid);
  151. // if(dict!=NULL){
  152. // /*int keyvalue=dict->valueForKey("key")->intValue();
  153. // CCLOG("keyvalue%d",keyvalue);
  154. // if(keyvalue==100){*/
  155. // return true;
  156. // //}
  157. // }
  158. //}
  159. CCPoint playerPoint = sprite->getPosition();
  160. CCLOG("playerPoint:%f,%f",playerPoint.x,playerPoint.y);
  161. CCPoint diff = ccpSub(touchPoint, playerPoint);
  162. if (abs(diff.x) > abs(diff.y))
  163. {
  164. if (diff.x > 0)
  165. {
  166. playerPoint.x += map->getTileSize().width;
  167. }
  168. else
  169. {
  170. playerPoint.x -= map->getTileSize().width;
  171. }
  172. }
  173. else
  174. {
  175. if (diff.y > 0 )
  176. {
  177. playerPoint.y += map->getTileSize().height;
  178. }
  179. else
  180. {
  181. playerPoint.y -= map->getTileSize().height;
  182. }
  183. }
  184. setPlayerPosition(playerPoint);
  185. setViewpointCenter(playerPoint);
  186.  
  187. return true;
  188. }
  189.  
  190. MyHelloWorld::~MyHelloWorld()
  191. {
  192. sprite->release();
  193. }
  194.  
  195. void MyHelloWorld::update(float)
  196. {
  197. /*CCLOG("update");
  198. float x,y;
  199. sprite->getPosition(&x,&y);
  200. CCLOG("%f,%f",x,y);*/
  201. }
  202.  
  203. /************************************************************************/
  204. /* 參数是精灵的位置 */
  205. /************************************************************************/
  206. void MyHelloWorld::setViewpointCenter(CCPoint& position) {
  207.  
  208. CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  209.  
  210. int x = MAX(position.x, winSize.width / 2);
  211. int y = MAX(position.y, winSize.height / 2);
  212. x = MIN(x, map->getMapSize().width * map->getTileSize().width - winSize.width / 2);
  213. y = MIN(y, map->getMapSize().height *map->getTileSize().height- winSize.height/2);
  214. CCPoint actualPosition = ccp(x, y);
  215.  
  216. CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
  217. CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
  218. CCLOG("viewPoint:%f,%f",viewPoint.x,viewPoint.y);
  219. this->setPosition(viewPoint);
  220.  
  221. /*CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  222. int x = MAX(position.x, winSize.width / 2);
  223. int y = MAX(position.y, winSize.height / 2);
  224.  
  225. x = MIN(x, map->getTileSize().width * map->getMapSize().height - winSize.width / 2);
  226. y = MIN(y, map->getTileSize().height * map->getMapSize().height - winSize.height / 2);
  227. CCPoint acutalPoint = ccp(x, y);
  228. CCPoint centerOfView = ccp(winSize.width / 2, winSize.height / 2);
  229. CCPoint ViewOfPoint = ccpSub(centerOfView, acutalPoint);
  230. this->setPosition(ViewOfPoint);*/
  231.  
  232. }
  233.  
  234. CCPoint MyHelloWorld::getTileCoordinate(CCPoint& point){
  235. float width=map->getTileSize().width;
  236. float height=map->getTileSize().height;
  237. CCLOG("getContentSize:%f",map->getContentSize().height);
  238. CCPoint re=ccp((int)(point.x/width),(int)((map->getContentSize().height-point.y)/height));
  239. return re;
  240. }
  241.  
  242. /************************************************************************/
  243. /* position 目标点 相对于layer的 */
  244. /************************************************************************/
  245. void MyHelloWorld::setPlayerPosition(cocos2d::CCPoint position)
  246. {
  247.  
  248. CCPoint tileCoord2 = this->tileCoordForGid(position);
  249. CCPoint tileCoord = this->getTileCoordinate(position);
  250. CCLOG("tileCoord:%f,%f",tileCoord.x,tileCoord.y);
  251. CCLOG("tileCoord2:%f,%f",tileCoord2.x,tileCoord2.y);
  252. int theGid = meta->tileGIDAt(tileCoord);
  253.  
  254. if (theGid)
  255. {
  256. CCLOG("gid:%d",theGid);
  257. CCDictionary* properties = map->propertiesForGID(theGid);
  258.  
  259. if (properties)
  260. {
  261. CCSprite* tempSprite=meta->tileAt(tileCoord);
  262. CCLOG("%d,",tempSprite->getZOrder());
  263. CCLOG("%d,",sprite->getZOrder());
  264. //sprite->setZOrder(0);
  265. //const CCString* str = properties->valueForKey("key");
  266. //CCLOG("STR:%d",str->intValue());
  267. //if (str && str->compare("200") == 0)
  268. ////if (str !=NULL)
  269. //{
  270. // return;
  271. //}
  272.  
  273. /*const CCString* tempstr = properties->valueForKey("Eat");
  274. if (tempstr && tempstr->compare("true") == 0)
  275. {
  276. _numCollected++;
  277. _hud->numCollectedChanged(_numCollected);
  278. _meta ->removeTileAt(tileCoord);
  279. _foodLayer->removeTileAt(tileCoord);
  280.  
  281. }*/
  282.  
  283. }
  284.  
  285. }
  286. //else{sprite->setZOrder(11);}
  287. sprite->setPosition(position);
  288. }
  289.  
  290. /************************************************************************/
  291. /返回的要转成int */
  292. /************************************************************************/
  293. CCPoint MyHelloWorld::tileCoordForGid(CCPoint position)
  294. {
  295. int x = position.x / map->getTileSize().width;
  296. CCLOG("tiled:%f",map->getMapSize().height * map->getTileSize().height);
  297. int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height ;
  298. return ccp(x, y);
  299.  
  300. }

cocos2dx游戏 地图的更多相关文章

  1. [转]eoe社区cocos2d-x游戏引擎知识大汇总

    [eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...

  2. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇

    懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...

  3. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析

    原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...

  4. 15款Cocos2d-x游戏源码

    (1)用cocos2d-x开发的中国象棋游戏源码 使用Cocos2d-X2.2.3开发的一款中国象棋游戏,游戏中可以实现.新局面.悔棋.游戏音乐.胜利后会显示游戏结果. 源码下载:http://www ...

  5. 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  6. 转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事

    原文地址:http://msopentech.com/zh-hans/blog/2014/05/09/cocos2d-x-%E6%B8%B8%E6%88%8F%E6%8E%A5%E5%85%A5-wi ...

  7. cocos2d-x游戏是怎么跑起来的

    虽然cocos2d-x v3.0 alpha版已经出来了,也改进了不少,有兴趣的可以去尝尝鲜.因为后面可能还会配合cocoStudio写一下博客,而现在v1.0.0.0版本需要配合cocos2d-x ...

  8. cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发

     cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622     ...

  9. Cocos2d-x游戏中默认的AndroidManifest.xml的解析

    直接上代码说明: <?xml version="1.0" encoding="utf-8"? > <!-- xmlns:android=&qu ...

随机推荐

  1. SQL常用语句|创建表,设置主键......

    新建表 create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' nu ...

  2. 解决虚拟机安装tomcat主机访问不到

    在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚拟机中可以访问,但是主机却无法访问,但是同时主机和虚拟机之间可以ping的通.解决方法是关闭虚拟机中的防火墙服务.桌面- ...

  3. 【spring boot】集成了druid后,同样的mybatis模糊查询语句出错Caused by: com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'name LIKE '%' ? '%'

    druid版本是 <!-- https://mvnrepository.com/artifact/com.alibaba/druid 数据库连接池--> <dependency> ...

  4. hdu4099 Revenge of Fibonacci

    题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...

  5. tmux用法

    列出所有的tmux  session,一个session是多个窗口的集合 tmux list-session 创建tmux窗口, tmux new -s server server为tmux的sess ...

  6. C# HttpWebRequest 绝技 【转】

    原文地址:http://www.sufeinet.com/thread-6-1-1.html 在线测试工具http://www.sufeinet.com/thread-3690-1-1.html c# ...

  7. Chromatix

    1.Lens Rolloff Correction  透镜衰减矫正 The Lens Rolloff correction takes into account the fact that,with ...

  8. Nginx auto_index和auth_basic

    Nginx auto_index和auth_basic 1.nginx auto_index nginx站点目录浏览功能,默认情况下为关闭 启用或禁用目录列表输出 开启这个功能的前提是站点目录下没有首 ...

  9. [Angular] ngPlural

    The usecase is very simple: <div [ngPlural]="items.length"> <ng-template ngPlural ...

  10. Angular 学习笔记——$http

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...