天天看点

Cocos3.4 横版游戏制作-《KillBear》-添加地图文件结构开始制作代码构建效果结语

转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

资源为网上寻找的,仅研究学习用,若是侵犯版权请通知本人整改。

此篇为第一篇,我们简单说明一下文件的结构.在新建一个文件的基础上,加入自制的瓦片地图.

开发环境

  • win64 : vs2010
  • Cocos2d-x v3.4Final
  • TexturePackerGUI
  • MapEdit

文件结构

Cocos3.4 横版游戏制作-《KillBear》-添加地图文件结构开始制作代码构建效果结语

参考的(转载的,我居然找不到原始博客了):

Cocos3.4 横版游戏制作-《KillBear》-添加地图文件结构开始制作代码构建效果结语

说明:

合理的文件结构有利于后续开发理解,之前犯了错,到最后结构混乱修改不便

开始制作

1.创建项目

在文件夹下Shift+右键,选择:在此处打开命令行窗口,新建一个项目

2.获取资源

我重新找了主角资源和地图资源,放在附件链接中.(之后可能会在此更新制作资源方法)

首先是地图,通过Tiled重新做了一个地图

Cocos3.4 横版游戏制作-《KillBear》-添加地图文件结构开始制作代码构建效果结语

地图有3层:

第一层是Wall,主角和敌人不能跑到墙上去,设定为7层

第二层是floor,主角主要在这里活动,设定为3层

第三层是BlackGround,拿来当填充背景的

每隔瓦片设定都是32X32大小的,地图总大小为80X10

代码构建

其他Other

APPDelegate

修改APPDelegate.cpp,符合我们的屏幕大小

auto director = Director::getInstance();
    auto eglView =director->getOpenGLView();
      if(!eglView) {
        eglView = GLViewImpl::create("My Game");
        eglView->setFrameSize(,);
        //WIN32下窗体大小,宽高
        director->setOpenGLView(eglView);
    }
    //director->setOpenGLView(eglView);
    eglView->setDesignResolutionSize(,, 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, );

    auto operateLayer = OperateLayer::create();
    scene->addChild(operateLayer, );

    auto statelayer =StateLayer::create();
    scene->addChild(statelayer);

    return scene;
}
           

状态State

StateLayer

bool StateLayer::init()
{
    bool ret = false;
    do {
        CC_BREAK_IF( !Layer::init() );

        ret = true;
    } while();

    return ret;
}
           

先空着

控制Operate

OperateLayer

bool OperateLayer::init()
{
    bool ret = false;
    do {
        CC_BREAK_IF( !Layer::init() );

        ret = true;
    } while();

    return ret;
}
           

同上

游戏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();

    return ret;
}
void MapLayer::initMapWithFile(const char * path)
{
    TMXTiledMap *TileMap = TMXTiledMap::create(path);
    TileMap->setPosition(Vec2(,));
    this->addChild(TileMap); 
    global->tileMap = TileMap;
}
           

本体GameLayer

  • .h
#ifndef _GAME_LAYER_H_
#define _GAME_LAYER_H_
#include "cocos2d.h"
USING_NS_CC;
#include "MapLayer.h"
class GameLayer : public Layer
{
public:
    GameLayer();
    ~GameLayer();
    virtual bool init();
    CREATE_FUNC(GameLayer);
};
           
  • .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,-);
        ret = true;
    } while();
    return ret;
}
           

效果

至此我们的第一步已经完成了,运行下可以看到一个窗口中间有我们自己制作的地图:

Cocos3.4 横版游戏制作-《KillBear》-添加地图文件结构开始制作代码构建效果结语

结语

这是最简单的部分了,下一篇将加入一个Hero

继续阅读