地图系统

来源:互联网 发布:想找淘宝客服工作 编辑:程序博客网 时间:2024/05/06 11:24
    //创建地图并添加    auto map = TMXTiledMap::create("TileMaps/orthogonal-test1.tmx");    addChild(map, 0, kTagTileMap);    //得到地图上的图层 并遍历    auto& children = map->getChildren();    SpriteBatchNode* child = nullptr;    for(const auto &obj : children) {        child = static_cast<SpriteBatchNode*>(obj);        //图片在放大的时候会出现锯齿。纹理类提供了        //setAntiAliasTexParameters()函数来处理抗锯齿        child->getTexture()->setAntiAliasTexParameters();    }    //从地图中根据图层名字获取图层    auto layer = map->getLayer("Layer 0");    //从图层上根据位置获取精灵    auto s = layer->getLayerSize();    Sprite* sprite;    sprite = layer->getTileAt(Vec2(0,0));    sprite->setScale(2);//放大    sprite = layer->getTileAt(Vec2(s.width-1,0));//右上角    sprite->setScale(2);    //得到指定位置的GID  GID将代表这个位置的东西    auto c = layer->getTileGIDAt(Vec2(0, 6));    auto b = layer->getTileGIDAt(Vec2(1, 6));    //用GID代表的东西 替换掉指定位置    layer->setTileGID(c, Vec2(1, 6));    layer->setTileGID(b, Vec2(0, 6));    auto c = layer->getTileGIDAt(Vec2(0, 6),(TMXTileFlags*)&flags);    if (flags & kTMXTileVerticalFlag)        flags &= ~kTMXTileVerticalFlag;    else        flags |= kTMXTileVerticalFlag;    layer->setTileGID(c, Vec2(0, 6), (TMXTileFlags)flags);    //给地图添加精灵并设置层次    Sprite *_tamara = Sprite::create(s_pathSister1);    map->addChild(_tamara, (int)map->getChildren().size() );    _tamara->retain();    int mapWidth = map->getMapSize().width * map->getTileSize().width;    _tamara->setPosition(CC_POINT_PIXELS_TO_POINTS(Vec2( mapWidth/2,0)));    _tamara->setAnchorPoint(Vec2(0.5f,0));    int newZ = 4 ;    //设置Z轴 用于精灵被挡住     map->reorderChild(_tamara, newZ);       //获取对象层 并遍历 改变    auto group = map->getObjectGroup("Object Group 1");    auto& objects = group->getObjects();    Value objectsVal = Value(objects);    CCLOG("%s", objectsVal.getDescription().c_str());    auto drawNode = DrawNode::create();    for (auto& obj : objects)    {        ValueMap& dict = obj.asValueMap();        float x = dict["x"].asFloat();        float y = dict["y"].asFloat();        float width = dict["width"].asFloat();        float height = dict["height"].asFloat();        Color4F color(1.0, 1.0, 1.0, 1.0);        drawNode->drawLine( Vec2(x, y), Vec2((x+width), y), color );        drawNode->drawLine( Vec2((x+width), y), Vec2((x+width), (y+height)), color );        drawNode->drawLine( Vec2((x+width), (y+height)), Vec2(x, (y+height)), color );        drawNode->drawLine( Vec2(x, (y+height)), Vec2(x, y), color );    }    map->addChild(drawNode, 10);    auto map = TileMapAtlas::create("TileMaps/tiles.png", "TileMaps/levelmap.tga", 16, 16);    addChild(map, 0, kTagTileMap);    schedule(CC_SCHEDULE_SELECTOR(HelloWorld::updateMap), 0.2f);void HelloWorld::updateMap(float dt){    auto tilemap = (TileMapAtlas*)getChildByTag(kTagTileMap);    //    // For example you can iterate over all the tiles    // using this code, but try to avoid the iteration    // over all your tiles in every frame. It's very expensive    //    for(int x=0; x < tilemap.tgaInfo->width; x++) {    //        for(int y=0; y < tilemap.tgaInfo->height; y++) {    //            Color3B c =[tilemap tileAt:Size(x,y));    //            if( c.r != 0 ) {    //                ////----CCLOG("%d,%d = %d", x,y,c.r);    //            }    //        }    //    }    // NEW since v0.7    Color3B c = tilemap->getTileAt(Vec2(13, 21));    c.r++;    c.r %= 50;    if (c.r == 0)        c.r = 1;    // NEW since v0.7    tilemap->setTile(c, Vec2(13, 21));}
0 0
原创粉丝点击