auto map = TMXTiledMap::create("map.tmx"); //加载地图 auto mapSize = map->getMapSize(); //获取地图大小 this->addChild(map); //添加到当前 auto mapPropertIEs = map->getPropertIEs(); //获取地图属性 cclOG("%s",mapPropertIEs["type"].asstring().c_str()); //获取type auto sceneLayer = map->getLayer("scene"); //获取图层scene auto layerPropertIEs = sceneLayer->getPropertIEs(); //获取图层信息 cclOG("%s",layerPropertIEs["type"].asstring().c_str()); auto tilePropertIEs = map->getPropertIEsForGID(1).asValueMap(); //获取第一个图块的信息 cclOG("%s",tilePropertIEs["type"].asstring().c_str()); auto objectGroup = map->getobjectGroup("object"); //获取object对象的对象组 auto objectPlayer = objectGroup->getobject("player"); //获取player对象 cclOG("%s",objectPlayer["type"].asstring().c_str()); auto objectcolor = objectGroup->getobject("color"); //获取color对象 auto colorX = objectcolor["x"].asInt() / 32; auto colorY = objectcolor["y"].asInt() / 32; auto colorW = objectcolor["wIDth"].asInt() / 32; auto colorH = objectcolor["height"].asInt() / 32; for (int i = colorX; i < colorX + colorW; i++){ for (int k = colorY; k < colorY + colorH; k++){ auto sprite = sceneLayer->getTileAt(Vec2(i,mapSize.height - k - 1)); //获取瓦块 sprite->setcolor(color3B(255,0,0)); //修改瓦块 } } auto player = Sprite::create("c.png"); player->setposition(objectPlayer["x"].asfloat(),objectPlayer["y"].asfloat()); this->addChild(player); auto Listener = EventListenertouchOneByOne::create(); // Listener->ontouchBegan = [player](touch *touch,Event* event){ auto target = (TMXTiledMaP*)event->getCurrentTarget(); auto sceneLayer = target->getLayer("scene"); auto point = target->convertToNodeSpace(touch->getLocation()); //世界坐标转map本地坐标 point.x = int(point.x / 32); point.y = target->getMapSize().height - int(point.y / 32) - 1; auto tileGID = sceneLayer->getTileGIDAt(point); //获取瓦块的GID auto tile = target->getPropertIEsForGID(tileGID).asValueMap(); //获取图块信息 if (tile["move"].isNull() != true && tile["move"].asBool() == true){ //判断是否可以移动 player->setposition(target->convertToNodeSpace(touch->getLocation())); } return false; }; Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,map);总结
以上是内存溢出为你收集整理的cocos2dx TMX地图全部内容,希望文章能够帮你解决cocos2dx TMX地图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)