前端之家收集整理的这篇文章主要介绍了
cocos2dx TMX地图,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
auto map = TMXTiledMap::create("map.tmx");
auto mapSize = map->getMapSize();
this->addChild(map);
auto mapProperties = map->getProperties();
CCLOG("%s",mapProperties["type"].asString().c_str());
auto sceneLayer = map->getLayer("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");
auto objectPlayer = objectGroup->getObject("player");
CCLOG("%s",objectPlayer["type"].asString().c_str());
auto objectColor = objectGroup->getObject("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());
point.x = int(point.x / 32);
point.y = target->getMapSize().height - int(point.y / 32) - 1;
auto tileGID = sceneLayer->getTileGIDAt(point);
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);