TiledMap 使用

TiledMap 使用



代码:

void HelloWorld::testTileMap(){
    
    CCTMXTiledMap* map= CCTMXTiledMap::create("Untitled.tmx");//获取tmx文件
    map->setPosition(ccp(this->getContentSize().width/2-100, this->getContentSize().height/2-100));
    this->addChild(map);
    
    // 所有的地图默认是失真的,如果你想创建非失真砖块,你需要添加入下代码:
    //遍历所有的层(失真精灵管理者),并且设置他们为非失真
    CCArray * pChildrenArray = map->getChildren();
    CCSpriteBatchNode* child = NULL;
    CCObject* pObject = NULL;
    
    CCARRAY_FOREACH(pChildrenArray, pObject)
    {
        child = (CCSpriteBatchNode*)pObject;
        if(!child)
            break;
        child->getTexture()->setAntiAliasTexParameters();
    }
    
    CCTMXLayer* layer1=map->layerNamed("layer1");//获取map里面的图层,
    CCSprite* sp1=layer1->tileAt(ccp(3, 3));//获取图层上砖块的
    CCSprite* sp2=layer1->tileAt(ccp(1, 0));
    layer1->removeChild(sp1, true);//移除砖块
    layer1->removeChild(sp2, true);
    layer1->removeTileAt(ccp(9, 1));//根据坐标移除砖块
    //map->removeChild(layer1);
    
    unsigned int m_gid = layer1->tileGIDAt(ccp(5,5));//获取一个砖块A的GID
    layer1->setTileGID(m_gid, ccp(2,7));//设定特定坐标为砖块A
    
//     遍历一个layer
//    CCSize s = layer1->getLayerSize();
//    for( int x=0; x<s.width;x++) {
//        for( int y=0; y< s.height; y++ ) {
//            unsigned int tmpgid = layer1->tileGIDAt(ccp(x,y));
//            layer1->setTileGID(tmpgid+1,ccp(x,y));
//        }
//    }
    
    //MARK: ==
    CCTMXObjectGroup* group =map->objectGroupNamed("obj111");//获取对象组
    
    CCDictionary* dic = group->objectNamed("yang");//获取对象组中的对象
    int x=dic->valueForKey("x")->intValue();//获取x坐标
    int y=dic->valueForKey("y")->intValue();//获取y坐标
    CCSprite* pSprite = CCSprite::create("CloseNormal.png");
    map->addChild(pSprite);//往map上添加一个精灵
    pSprite->setPosition(ccp(x, y));//设置这个精灵的位置
    
//    // 对象层 3.0以后使用方法
//    TMXObjectGroup* pipeGroup = map->objectGroupNamed("pipe");
//    //得一个
//    ValueMap pipe_1  = pipeGroup->objectNamed("pipe_1");
//    std::string pipeStr = pipe_1["name"].asString();
//    float pipeX = pipe_1["x"].asFloat();
//    float pipeY= pipe_1["y"].asFloat();;
//    float pipeWidht = pipe_1["width"].asFloat();
//    float pipeHeight = pipe_1["height"].asFloat();
    
    CCDictionary* dic2 = group->objectNamed("xuan");
    int x2=dic2->valueForKey("x")->intValue();
    int y2=dic2->valueForKey("y")->intValue();
    CCSprite* pSprite2 = CCSprite::create("CloseNormal.png");
    map->addChild(pSprite2);
    pSprite2->setPosition(ccp(x2, y2));
    
    CCTMXObjectGroup* g2=map->objectGroupNamed("obj222");
    
    CCDictionary* dic4=g2->objectNamed("uuu");
    int x4 = dic4->valueForKey("x")->intValue();
    int y4 = dic4->valueForKey("y")->intValue();
    CCSprite* pSprite4 = CCSprite::create("CloseNormal.png");
    map->addChild(pSprite4);
    pSprite4->setPosition(ccp(x4, y4));
    
    CCDictionary* dic5=g2->objectNamed("ooo");
    int x5 = dic5->valueForKey("x")->intValue();
    int y5 = dic5->valueForKey("y")->intValue();
    CCSprite* pSprite5 = CCSprite::create("CloseNormal.png");
    map->addChild(pSprite5);
pSprite5->setPosition(ccp(x5, y5));
}

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值