Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植9:Map Loop【地图循环加载】
来源:互联网 发布:js获取json对象的属性 编辑:程序博客网 时间:2024/05/11 06:06
尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17383997
<捕鱼达人>回顾
【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍
<城市跑酷>回顾
【cocos2d-x IOS游戏开发-城市跑酷1】跑酷游戏介绍
上节回顾
Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植8: Jumping and Crouching
目前为止,游戏角色还很孤独的跑在一个黑色的世界,现在要做的是给游戏添加背景图片。 背景由上下两部分组成,当游戏角色在两张图片中间的夹缝上跑动的时候,第一张背景图片会慢 慢被第二张背景图片替换,第一张图片将重新加载。
创建一个名为Map.cpp到VS工程里,添加下面的代码:
#include "Map.h"#include "resource.h"#include "Utils.h"Map::~Map(){}Map::Map(){}/** Constructor * @param {cc.Layer *} * @param {cp.Space *} */Map::Map(CCLayer *layer, cpSpace *space){this->layer = NULL;this->space = NULL;//MapCount应等于在资源文件夹中的文件数量。并应不少于两个this->mapCount = 2;this->spriteWidth = 0;this->map0 = NULL;this->map1 = NULL;this->ground0 = NULL;this->ground1 = NULL;this->curMap = 0;this->layer = layer;this->space = space;// init bg map for the first time//地图的上半部分是一个锚点改为(0,0)的精灵,用更改锚点来简化坐标的计算map0 = CCSprite::create("res/Map00.png");map0->setAnchorPoint(ccp(0, 0));map0->setPosition(ccp(0, 0));layer->addChild(map0);//上层部分跟下层部分的不同之处是它们的位置坐标,将下层部分Y轴坐标设置为 //g_groundHight – ground0->getContentSize().height来确保游戏角色的脚是踏在地面上的。ground0 = CCSprite::create("res/Ground00.png");ground0->setAnchorPoint(ccp(0, 0));CCSize size = ground0->getContentSize();ground0->setPosition(ccp(0, g_groundHight - size.height));layer->addChild(ground0);spriteWidth = map0->getContentSize().width;map1 = CCSprite::create("res/Map01.png");map1->setAnchorPoint(ccp(0, 0));// 第二张地图的开始位置是背景的宽度map1->setPosition(ccp(spriteWidth, 0));layer->addChild(map1);ground1 = CCSprite::create("res/Ground01.png");ground1->setAnchorPoint(ccp(0, 0));ground1->setPosition(ccp(spriteWidth, g_groundHight - size.height));layer->addChild(ground1);}/** * @return width of the map sprite. */ float Map::getMapWidth(){return spriteWidth;}/** * @return which map of the runner current run on. */ int Map::getCurMap() { return this->curMap;}/** * @param {float} X-coordinate of the eye of camera of PlayLayer */bool Map::checkAndReload(float eyeX) {//用这种方式计算地图坐标int newCur = int(eyeX / spriteWidth); if (curMap == newCur) { return false; } CCSprite * map; CCSprite * ground; if (0 == newCur % 2) { // change mapSecond map = this->map1; ground = this->ground1; } else { // change mapFirst map = this->map0; ground = this->ground0; } CCLOG("==load map:%d", (newCur + 1)); this->curMap = newCur;//在一张背景跑完之后需要切换一张新的图片 // load curMap + 1 //var fileName = "res/Map" + FormatNumberLength((newCur + 1) % mapCount, 2) + ".png";//char fileMapName[100] = {0};//sprintf(fileName, "res/Map%i.png", i);//fileMapName = "res/Map" + FormatNumberLength((newCur + 1) % mapCount, 2) + ".png";//CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(fileMapName);//暂时简化,待扩展std::string fileMapName;if ((newCur + 1) % mapCount){fileMapName = "res/Map00.png";} else{fileMapName = "res/Map01.png";}CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(fileMapName.c_str()); map->setTexture(texture); map->setPositionX(this->spriteWidth * (newCur + 1)); // load ground#if 0var fileGroundName = "res/Ground" + FormatNumberLength((newCur + 1) % mapCount, 2) + ".png";CCTexture2D *textureGround = CCTextureCache::sharedTextureCache()->addImage(fileGroundName);ground->setTexture(textureGround);ground->setPositionX(this->spriteWidth * (newCur + 1));#elsestd::string fileGroundName;if ((newCur + 1) % mapCount){fileGroundName = "res/Ground00.png";} else{fileGroundName = "res/Ground01.png";}CCTexture2D *textureGround = CCTextureCache::sharedTextureCache()->addImage(fileGroundName.c_str());ground->setTexture(textureGround);ground->setPositionX(this->spriteWidth * (newCur + 1));#endif return true;}
在PlayScene.cpp的里定义新的变量:
Map *map;
通过添加下面代码到init()初始化地图:
//初始化地图map = new Map(this, space);将下面代码添加到update()函数中:
// check and reload mapbackgroundLayerif (true == map->checkAndReload(lastEyeX)) {//if (true == backgroundLayer->checkAndReload(lastEyeX)) {objectManager->recycleObjectOfMap(map->getCurMap() - 1);objectManager->initObjectOfMap(map->getCurMap() + 1, map->getMapWidth());//level uprunner->levelUp();}
编译运行后你将看到这样的界面:
2 1
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植9:Map Loop【地图循环加载】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植11:添加金币和障碍物到地图中
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植4:游戏主场景的介绍
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植1:环境搭建及Parkour介绍
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植5:初始化物理世界【Chipmunk】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植6:Running This Way
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植7:Gesture Recognizer【手势识别】
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植8: Jumping and Crouching
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植3:游戏主菜单界面的实现
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植2:如何新建一个项目,引擎目录结构介绍
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植10:Adding Coins and Rocks【添加金币和障碍物】
- cocos2d-x游戏开发 跑酷(六) 地图循环
- cocos2d-x游戏开发 跑酷(六) 地图循环
- cocos2d-x 游戏 之 Tiled Map Editor(地图编辑)
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》第四步--地图循环&主角添加动作
- 泰然网Cocos2d-x文档教程和其他源码
- jni cocos2d-x游戏引擎移植到android:helloworld
- cocos2d-x游戏iphone移植到android需要注意事项
- Kali Linux渗透测试实战 1.1 Kali Linux简介
- 项目视频讲解_基于SpringSecurity3.x, JasperReport5.x等技术实现仿金蝶权限管理的企业信息管理系统
- 32位系统和64位系统变量所占空间总结
- java 自定义LineNumerReader
- hibernate调用mysql存储过程
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植9:Map Loop【地图循环加载】
- 宿主机与虚拟机互相ping通,宿主机ping通另一台机器的虚拟机
- 基于Hibernate的图片数据库存储(mysql)
- minix masterboot解读
- IOS UITableView 表视图的绝大部分方法
- 矩阵的理解
- JAVA笔记【20131217】
- 程序分析2
- 算法竞赛入门经典-前四章-UVa10055 - Hashmat the Brave Warrior-勇士Hashmat-难度1