24.Cocos2d-x瓦片地图TMXTiledMap

HelloWorldScene.h文件:


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp文件:


#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto rootNode = CSLoader::createNode("MainScene.csb");
//瓦片地图编辑器生成瓦片地图map.tmx;把相关资源放到工程文件夹下
    auto map1 = TMXTiledMap::create("map.tmx");
    addChild(map1);
    Vector<Node*> maps = map1->getChildren();

    SpriteBatchNode* child = NULL;
    Ref* pObject = NULL;
    for (Vector<Node*>::iterator item = maps.begin(); item != maps.end();item++) {
        pObject = *item;
        child = (SpriteBatchNode*)pObject;
        //CCLOG("%d", child->get));
    }
    TMXLayer* layer = map1->getLayer("scene");






    addChild(rootNode);

    return true;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
tIDE是多国语言的免费软件,但是!竟然没有中文,关于它的介绍自己看就不说了,下面来创建第一张地图,用于我们的TD游戏。 你现在需要一张这样的tile图片,来做完地图的基本元件 在Explorer面板Map节点地下的Tile Sheets右键Add,然后浏览选择上面的或者你自己的tile文件。 OK弹出Tile Sheet Properties对话框设置一下Tile Size让Tile完美的切割。这里的值是60x60。 ok,然后会在Tile picher面板里就会出现一个个分割好的Tile。 有了这些tile的支持就可以在map上玩拼图了。在map Explorer面板下的Map节点的Layers上右键Add,在弹出的Layer Properties设置地图的大小Layer Size,因为我现在只打算做一个最简单的也就是一屏幕而且不能拖动所以这里设置14x8 (800/60 x 480/60),OK。 为了方便绘制在工具面板的右上角有一个显示网格的按钮,点下去它 右边的绘图工具栏上有一个很好使用的按钮区域绘制,它能把选择区域填充出当前选择的tile,下面就先在tile Picker选择一块浅色的草地区域绘制到整个地图。然后画上我们的路。 Save as为 tmx类型的文件 中间如果出现警告确定就好了 这里出现了最关键的一步,这里一定要选Base64(gzip compressed),ccx现在的版本还只支持这一种。 把生成的tmx文件以及tile图片加入我们的项目中。 这里可能需要调整一下tmx文件中的Image source的路径 在程序中显示TMX地图 先显示出来看看努力的效果,之后再慢慢完善。 把GameScreen构造函数中的说明Label删除,添加CCTMXTiledMap 代码很简单如下 private GameScreen() { CCTMXTiledMap tmxmap = CCTMXTiledMap.tiledMapWithTMXFile("Map/Level1"); addChild(tmxmap); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值