TMXTiledMap 增加碰撞体,可以添加任何所见到的物体上,本次文章是增加到object多边形上,TMXTiledMap必须存在object,代码如下
TMXTiledMap * map = TMXTiledMap::create("gismap.tmx");
TMXLayer * layer = map->getLayer("layer");
TMXObjectGroup * group = map->getObjectGroup("object1");
ValueVector objects = group->getObjects();
Color4F color(1.0, 1.0, 1.0, 1.0);
auto s = layer->getLayerSize();
this->addChild(map);
//获取单一对象可以使用qq1必须存在
//ValueMap valuem = group->getObject("qq1");
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();
PhysicsBody * phy = PhysicsBody::createBox(Size(width, height));
phy->setDynamic(false);
Sprite * sp = Sprite::create();
sp->setPosition(Vec2(x,y));
sp->setAnchorPoint(Vec2::ZERO);
Size sss = sp->getContentSize();
sp->setContentSize(Size(width, height));
sp->setPhysicsBody(phy);
this->addChild(sp);
}