制作横版游戏KillBear第1课:添加地图
在这个系列中,我们将通过制作横版游戏《KillBear》,深入学习游戏制作中的关键逻辑和实现方法。本篇是该系列的第一课,我们将简要说明文件结构,并在新建文件的基础上,添加自制的瓦片地图。
开发环境
- 操作系统:Win64
- 开发工具:vs2010
- 游戏引擎:Cocos2d - x v3.4Fi
- 地图编辑工具:MapEdit
文件结构
合理的文件结构有助于后续开发和理解。之前在开发过程中,若文件结构不合理,可能会导致后期结构混乱,修改困难。因此,我们在开始项目前,要规划好文件结构。
开始制作
1. 创建项目
在指定文件夹下,按下 Shift 键并点击鼠标右键,选择“在此处打开命令行窗口”,然后新建一个项目。
2. 获取资源
我重新收集了主角资源和地图资源,这些资源可以通过附件链接获取(之后可能会在此更新制作资源的方法)。
首先是地图资源,我们使用 Tiled 重新制作了一个地图。该地图有 3 层:
- 第一层(Wall):主角和敌人无法跑到墙上去,此层设定为 7 层。
- 第二层(Floor):主角主要在此层活动,设定为 3 层。
- 第三层(BlackGround):用于填充背景。
每个瓦片的大小设定为 32×32,地图的总大小为 80×10。
代码构建
其他(Other) - APPDelegate
修改 APPDelegate.cpp 文件,使其符合我们的屏幕大小,具体代码如下:
auto director = Director::getInstance();
auto eglView = director->getOpenGLView();
if (!eglView) {
eglView = GLViewImpl::create("My Game");
eglView->setFrameSize(480, 320);
// WIN32 下窗体大小,宽高
director->setOpenGLView(eglView);
}
// director->setOpenGLView(eglView);
eglView->setDesignResolutionSize(480, 320, ResolutionPolicy::SHOW_ALL);
// ...
// create a scene. it's an autorelease object
auto scene = GameScene::createScene();
// run
director->runWithScene(scene);
GameScene
在 GameScene 中添加我们需要的层,代码如下:
Scene* GameScene::createScene()
{
auto scene = Scene::create();
auto gamelayer = GameLayer::create();
scene->addChild(gamelayer, 0);
auto operateLayer = OperateLayer::create();
scene->addChild(operateLayer, 1);
auto statelayer = StateLayer::create();
scene->addChild(statelayer);
return scene;
}
状态(State) - StateLayer
StateLayer 的初始化代码如下:
bool StateLayer::init()
{
bool ret = false;
do {
CC_BREAK_IF(!Layer::init());
ret = true;
} while (0);
return ret;
}
目前该层的功能暂未实现,先留空。
控制(Operate) - OperateLayer
OperateLayer 的初始化代码如下:
bool OperateLayer::init()
{
bool ret = false;
do {
CC_BREAK_IF(!Layer::init());
ret = true;
} while (0);
return ret;
}
与 StateLayer 类似,该层目前也暂未实现具体功能。
游戏(Game)
游戏层包含地图层,下面分别介绍地图层和游戏本体层的代码。
地图层(MapLayer)
- 头文件(.h)
class MapLayer : public Layer { public: MapLayer(); ~MapLayer(); virtual bool init(); void update(float dt); void setViewpointCenter(Point pos); CREATE_FUNC(MapLayer); private: void initMapWithFile(const char * path); }; - 实现文件(.cpp)
bool MapLayer::init() { bool ret = false; do { CC_BREAK_IF(!Layer::init()); this->initMapWithFile("mymap.tmx"); // 地图初始化 ret = true; } while (0); return ret; }
void MapLayer::initMapWithFile(const char path) { TMXTiledMap TileMap = TMXTiledMap::create(path); TileMap->setPosition(Vec2(0, 0)); this->addChild(TileMap); global->tileMap = TileMap; }
#### 本体(GameLayer)
- **头文件(.h)**
ifndef _GAME_LAYERH
define _GAME_LAYERH
include "cocos2d.h"
USING_NS_CC;
include "MapLayer.h"
class GameLayer : public Layer { public: GameLayer(); ~GameLayer(); virtual bool init(); CREATE_FUNC(GameLayer); };
endif
- **实现文件(.cpp)**
bool GameLayer::init() { bool ret = false; do { CC_BREAK_IF(!Layer::init()); _visibleSize = Director::getInstance()->getVisibleSize(); _visibleOrigin = Director::getInstance()->getVisibleOrigin(); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Boy.plist", "Boy.pvr.ccz"); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Enemy.plist", "Enemy.pvr.ccz"); auto map = MapLayer::create(); this->addChild(map, -100); ret = true; } while (0); return ret; }
## 效果
至此,我们的第一步已经完成。运行项目后,你将看到一个窗口,窗口中间显示着我们自己制作的地图。
## 结语
这是整个游戏制作过程中较为简单的部分。在下一篇文章中,我们将为游戏添加一个英雄角色。