地图

新建项目名为testMap。

cocos2dx常用的地图编辑器是Tield Map Editor,将它保存好的地图文件map1.tmx放入项目中。

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();  


    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

};


#endif // __HELLOWORLD_SCENE_H__


修改HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *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 ( !CCLayer::init() )

    {

        return false;

    }


    //通过地图编辑器生成的地图

    CCTMXTiledMap* map=CCTMXTiledMap::create("map1.tmx");

    addChild(map);

    

    CCSize tileSize=map->getTileSize();

    CCSize mapSize=map->getMapSize();

    CCLOG("tileSize:%f_%f,mapSize:%f_%f",tileSize.width,tileSize.height,mapSize.width,mapSize.height);

    

    //遍历每个图块并设置抗锯齿

    CCArray *pChildrenArray=map->getChildren();

    CCSpriteBatchNode *child=NULL;

    CCObject *pObject=NULL;

    CCARRAY_FOREACH(pChildrenArray, pObject)

    {

        child=(CCSpriteBatchNode*) pObject;

        if (!child) {

            break;

        }

        child->getTexture()->setAntiAliasTexParameters();

    }

    //取出地图层中的最后一个图块进行操作

    CCTMXLayer *layer=map->layerNamed("layer1");

    CCSprite *sprite=layer->tileAt(ccp(9,9));

    sprite->setScale(2);

    sprite->setRotation(-90);

    

    

    return true;

}




你可能感兴趣的:(地图)