• Cocos2d TileMaps 学习笔记 [转]


    转自 http://blog.chinaunix.net/uid-20622737-id-2383214.html

    //------------------------------------------------------------------

    CCTMXTiledMap 中的图层是从下到上 依次刷新屏幕的。 最底层的 layer 最先显示。 上面层的 layer 将会覆盖下面层的显示。 

    如下图所示:刷新屏幕显示的顺序依次是: grass, tress4, tress3, tress2. 后刷新的图层将会覆盖前面的显示。

    //------------------------------------------------------------------

    // 将当前的地图层中,将某一个网格处的图片显示转换为   CCSprite 对象

    CCSprite * m_sprite = mapLayer->tileAt( ccp(29,29) );  //   ccp(29,29)  为地图网格坐标
    m_sprite->retain();

    //------------------------------------------------------------------

    CCTMXTiledMap *map = CCTMXTiledMap::tiledMapWithTMXFile("TileMaps/test.tmx"); // 加载地图
    addChild(map, 0, kTagTileMap);

     CCSize s = map->getContentSize();  // 地图显示区域大小

    // because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you
    // can use any CCSprite and it will work OK.
     CCTMXLayer* layer = map->layerNamed("grass");  // 得到 名字为 grass 的地图层的数据  
      CCSprite * m_sprite = layer->tileAt(ccp(0,11)); // 将当前图层中, 地图网格位置 0,11 处的单元显示转换为 一个 CCSprite, 次 sprite 已经随地图进行    addChild 了。 此处不需要在调用   addChild  函数。 

    m_sprite->retain();

    //------------------------------------------------------------------ 

    // 设置地图的旋转视角(三维的)

    CCTMXTiledMap* map = CCTMXTiledMap::tiledMapWithTMXFile("TileMaps/orthogonal-test2.tmx");
    addChild(map, 0, kTagTileMap);

    CCSize s = map->getContentSize();
    ////----UXLOG("ContentSize: %f, %f", s.width,s.height);

    CCArray * pChildrenArray = map->getChildren();
    CCSpriteBatchNode* child = NULL;
    CCObject* pObject = NULL;
    CCARRAY_FOREACH(pChildrenArray, pObject)
    {
    child = (CCSpriteBatchNode*)pObject;

    if(!child)
    break;

    //sets antialias texture parameters 设置反锯齿的纹理参数
    child->getTexture()->setAntiAliasTexParameters();
    }

    float x, y, z;
    map->getCamera()->getEyeXYZ(&x, &y, &z);
    map->getCamera()->setEyeXYZ(x-200, y, z+300);

    效果图:

    //------------------------------------------------------------------

    // 地图的缩放效果

    CCTMXTiledMap* map = CCTMXTiledMap::tiledMapWithTMXFile("TileMaps/orthogonal-test1.tmx");
    addChild(map, 0, kTagTileMap);

    CCSize s = map->getContentSize();
    ////----UXLOG("ContentSize: %f, %f", s.width,s.height);

    CCArray* pChildrenArray = map->getChildren();
    CCSpriteBatchNode* child = NULL;
    CCObject* pObject = NULL;
    CCARRAY_FOREACH(pChildrenArray, pObject)
    {
    child = (CCSpriteBatchNode*)pObject;

    if(!child)
    break;
    // 对每一个结点纹理设置 反锯齿 效果
    child->getTexture()->setAntiAliasTexParameters();
    }

    map->runActionCCScaleBy::actionWithDuration(2, 0.5f) ) ;  //  地图缩小动画

    map->setScale(0.2f);  // 设置地图缩放

    map->setAnchorPoint( ccp(0.5f, 0.5f) ); // 设置地图的锚点

    //------------------------------------------------------------------

    //  地图拖动代码片段

    void TileDemo::ccTouchMoved(CCTouch* touch, CCEvent* event)
    {
    CCPoint touchLocation = touch->locationInView( touch->view() );   // 当前的触摸点
    CCPoint prevLocation = touch->previousLocationInView( touch->view() );  // 上一个触摸点

    touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
    prevLocation = CCDirector::sharedDirector()->convertToGL( prevLocation );

    CCPoint diff = ccpSub(touchLocation, prevLocation);

    CCNode *node = getChildByTag(kTagTileMap);  // 得到地图结点
    CCPoint currentPos = node->getPosition();
    node->setPosition( ccpAdd(currentPos, diff) );
    }

    //------------------------------------------------------------------ 

     CCSize ms = map->getMapSize();  // 地图大小(x,y 方向上地图块的个数)
     CCSize ts = map->getTileSize(); // 每一个地图土块的宽高(像素)

      CCSize s = map->getContentSize(); // 整个地图在屏幕上的显示区域大小(像素)

    getContentSize  的大小计算:

    content_width = map_width *map_title_width;

    conten_height = map_height * map_title_height;

    //------------------------------------------------------------------

    // 地图中对象层的使用 CCTMXObjectGroup

    // 加载地图

    CCTMXTiledMap *map = CCTMXTiledMap::tiledMapWithTMXFile("TileMaps/ortho-objects.tmx");
    addChild(map, -1, kTagTileMap);

    CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
     CCTMXObjectGroup* group = map->objectGroupNamed("Object Group 1");  // 得到地图中的对象层数据

    CCMutableArray<CCStringToStringDictionary*> * objects = group->getObjects();
    CCStringToStringDictionary* dict;
    CCMutableArray<CCStringToStringDictionary*>::CCMutableArrayIterator it;

    // 变量当前对象层中的所有对象, 这里得到了对象层中的 x,y,width, height 等属性值
    for( it = objects->begin(); it != objects->end(); it++) 
    {
    dict = (*it);//dynamic_cast<CCStringToStringDictionary*>(*it);

    if(!dict)
    break;
    std::string key = "x";
    int x = dict->objectForKey(key)->toInt();  // 得到一个属性值
    key = "y";
    int y = dict->objectForKey(key)->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("y"))->getNumber();
    key = "width";
    int width = dict->objectForKey(key)->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("width"))->getNumber();
    key = "height";
    int height = dict->objectForKey(key)->toInt();//dynamic_cast<NSNumber*>(dict->objectForKey("height"))->getNumber();

     }

    对象层中的属性信息:

    // 地图编辑器中,设置对象的属性信息, 在此界面中可以设置或者添加对象的属性信息, 比如我们这里添加了一个 friction 的属性

    //------------------------------------------------------------------ 

    // 地图和地图层的属性信息获得

    注释: 地图 和地图层的属性信息也可以设置, 他们的属性信息分配存储在各自的 m_pProperties->m_map 数据成员中。

    截图如下:aaa, bbb 就是设置的属性信息



    layer->getProperties()    // 得到一个层的属性信息

    map->getProperties()   // 得到地图的属性信息

    CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);

    CCMutableArray<CCStringToStringDictionary*> * objects = map -> getProperties ();
    CCStringToStringDictionary* dict;
    CCMutableArray<CCStringToStringDictionary*>::CCMutableArrayIterator it;

    // 遍历所有的属性
    for( it = objects->begin(); it != objects->end(); it++) 
    {
    dict = (*it);//dynamic_cast<CCStringToStringDictionary*>(*it);

    if(!dict)
    break;


    std::string key = "x";
    int x = dict->objectForKey(key)->toInt();  // 得到一个属性值
      
     }

  • 相关阅读:
    WebServices Get
    字符出现次数
    正则
    防止AutoPost
    转双问号,单问号
    GetData
    UpdatePanel
    字居中
    C# 面向对象之多态
    C# 委托之把委托从委托链(多播委托)移除
  • 原文地址:https://www.cnblogs.com/afly/p/2390393.html
Copyright © 2020-2023  润新知