对于tiledmap的一些操作

来源:互联网 发布:折装导轮的工具的数据 编辑:程序博客网 时间:2024/06/17 15:56

1.

<span style="font-size:14px; font-family: Arial, Helvetica, sans-serif;">auto map = TMXTiledMap::create("TileMaps/orthogonal-test1.tmx");</span>

<span style="font-size:14px;">addChild(map, 0, 0);</span>   <span style="font-size:14px;"> Size CC_UNUSED s = map->getContentSize();    CCLOG("ContentSize: %f, %f", s.width,s.height);    //遍历地图上的全部对象,并开启搞锯齿    auto& children = map->getChildren();    SpriteBatchNode* child = nullptr;    for(const auto &obj : children) {        child = static_cast<SpriteBatchNode*>(obj);        child->getTexture()->setAntiAliasTexParameters();    }</span>

2.得到地图上的单个图块对象并从地图上移除:

     <span style="font-size:14px;">auto map = static_cast<TMXTiledMap*>( getChildByTag(kTagTileMap) );    auto layer = map->getLayer("Layer 0");    auto s = layer->getLayerSize();    auto sprite = layer->getTileAt( Vec2(s.width-1,0) );    auto sprite2 = layer->getTileAt(Vec2(2,63));    layer->removeChild(sprite, true);    auto sprite3 = layer->getTileAt(Vec2(2, s.height-1));    layer->removeChild(sprite3, true);    layer->removeChild(sprite2, true);</span>

3.移动地图:

<span style="font-size:14px;">auto ms = map->getMapSize();auto ts = map->getTileSize();map->runAction( MoveTo::create(1.0f, Vec2( -ms.width * ts.width/2, -ms.height * ts.height/2 ) ));</span>
4.精灵在地图上移动,交换Z轴

    <span style="font-size:14px;">auto map = TMXTiledMap::create("TileMaps/orthogonal-test-zorder.tmx");    addChild(map, 0, 0);    Size CC_UNUSED s = map->getContentSize();    CCLOG("ContentSize: %f, %f", s.width,s.height);    _tamara = Sprite::create("Images/grossinis_sister1.png");    map->addChild(_tamara,  (int)map->getChildren().size());    _tamara->retain();    _tamara->setAnchorPoint(Vec2(0.5f,0));    auto move = MoveBy::create(10, Vec2(400,450));    auto back = move->reverse();    auto seq = Sequence::create(move, back,nullptr);    _tamara->runAction( RepeatForever::create(seq));    schedule( schedule_selector(HelloWorld::repositionSprite));</span>
<pre name="code" class="cpp"><span style="font-size:14px;">void HelloWorld::repositionSprite(float dt){    auto p = _tamara->getPosition();    p = CC_POINT_POINTS_TO_PIXELS(p);    auto map = getChildByTag(0);    int newZ = 4 - ( (p.y-10) / 81);    newZ = std::max(newZ,0);    map->reorderChild(_tamara, newZ);}</span>

效果图如下:



5.遍历地图上的精灵:

<span style="font-size:14px;">auto& children = map->getChildren();    for(const auto &node : children) {        auto child = static_cast<SpriteBatchNode*>(node);        child->getTexture()->setAntiAliasTexParameters();    }</span>
6.遍历对象层:

<span style="font-size:14px;">auto group = map->getObjectGroup("Object Group 1");    auto& objects = group->getObjects();    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();                glLineWidth(3);             }</span>























0 0
原创粉丝点击