不解释直接上代码
#include "TmxMapBasic.h"
CCScene* TmxMapBasic::scene()
{
CCScene* s = CCScene::create();
TmxMapBasic* layer = TmxMapBasic::create();
s->addChild(layer);
return s;
}
bool TmxMapBasic::init()
{
LayerBack::init();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCTMXTiledMap* map = CCTMXTiledMap::create("orthogonal-test1.tmx");
addChild(map);
// map->setScaleX(winSize.width / map->getContentSize().width);
// map->setScaleY(winSize.height / map->getContentSize().height);
setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne);
_map = map;
return true;
}
bool TmxMapBasic::ccTouchBegan(CCTouch* touch, CCEvent*)
{
CCPoint ptLocation = touch->getLocation();
// ptLocation -> 在tmx地图里的坐标
CCSize tileSize = _map->getTileSize();
CCSize mapSize = _map->getMapSize();
CCPoint ptInMap;
ptInMap.y = mapSize.height* tileSize.height - ptLocation.y;
ptInMap.x = ptLocation.x;
int tx = ptInMap.x / tileSize.width;
int ty = ptInMap.y / tileSize.height;
#if 0
CCTMXLayer* layer0 = _map->layerNamed("Layer 0");
layer0->setTileGID(9, ccp(tx, ty));
CCSprite* sp = layer0->tileAt(ccp(tx, ty));
CCAction* action = CCJumpBy::create(3, ccp(0, 0), 30, 5);
sp->runAction(action);
#endif
// CCTMXLayer* layer1 = _map->layerNamed("Layer 1");
// layer1->setTileGID(9, ccp(tx, ty));
#if 1
CCTMXObjectGroup* objG = _map->objectGroupNamed("OBJECT1");
CCArray* objs = objG->getObjects();
int count = objs->count(); // 5
for (int i = 0; i < 5; i++)
{
CCObject* obj = objs->objectAtIndex(i);
CCDictionary* dict = (CCDictionary*)obj;
const CCString* x = dict->valueForKey("x");
const CCString* y = dict->valueForKey("y");
const CCString* height = dict->valueForKey("height");
CCLog("x is %d, y is %d, h is %d", x->intValue(), y->intValue(), height->intValue());
}
#endif
return true;
}
浙公网安备 33010602011771号