首页 > 编程知识 正文

地瓦片,瓦片地拼接

时间:2023-05-03 12:59:27 阅读:188314 作者:1753

1.加载地图,获取背景层,调整背景位置,获取其他对象

 

//获取地图 map=CCTMXTiledMap::create("tilemap1.tmx"); //设置地图背景层的位置 auto background=map->layerNamed("bg"); background->setAnchorPoint(ccp(0.5,0.5)); background->setPosition(visibleSize.width/2,visibleSize.height/2); //获得地图原点和屏幕原点的偏移 offX=(map->getContentSize().width-visibleSize.width)/2; offY=(map->getContentSize().height-visibleSize.height)/2; //显示地图 addChild(map,bgZ); //获取地图中的标记点 objects=map->objectGroupNamed("obj");CCDictionary*obj=objects->objectNamed("0");

 

2.将屏幕坐标转换为瓦片坐标

cocos2d::CCPointmyTowerDefeat::convertToMapPosision(CCPoint position){ intx=(position.x+offX)/map->getContentSize().width*map->getMapSize().width;//按照比例取瓦片地图的点 inty=map->getMapSize().height-(position.y+offY)/map->getContentSize().height*map->getMapSize().height;//纵坐标相反 return ccp(x,y);}

 

3.获取瓦片属性

//注意,这里的GID是跟瓦片类型绑定的,而不是地图中的第n个瓦片,即不同位置的瓦片的GID有可能是一样的 autogid=map->layerNamed("bg")->tileGIDAt(position); //获取瓦片的所有属性 autotileProperty=map->propertiesForGID(gid); //取出键对应的属性 if(tileProperty)canTouch=tileProperty->valueForKey("canTouch")->intValue();

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。