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;
}

你可能感兴趣的:(Cocos2d-x)