24.Cocos2d-x瓦片地图TMXTiledMap

前端之家收集整理的这篇文章主要介绍了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;
}
原文链接:https://www.f2er.com/cocos2dx/340416.html

猜你在找的Cocos2d-x相关文章