天天看點

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

繼續閱讀