bool MapLayer::init() { bool bRet = false; do { CC_BREAK_IF(!Layer::init()); Size winSize = Director::getInstance()->getWinSize(); // 初始化地图 TMXTiledMap* map = TMXTiledMap::create("iso-test-zorder.tmx"); map->setPosition((winSize.width - map->getContentSize().width)/2, 0); this->addChild(map, 0, kTagTileMap); // 初始化任务 _tamara = Sprite::create("grossinis_sister1.png"); map->addChild(_tamara, map->getChildren().size()); _tamara->retain(); int mapWidth = map->getMapSize().width * map->getTileSize().width; int mapHeight = map->getMapSize().height * map->getTileSize().height; _tamara->setPosition(mapWidth/2, 112); _tamara->setAnchorPoint(Point(0.5f, 0)); _vmove = -1; _hmove = -1; _stepIndex = -1; _myAstar = new Astar(); this->scheduleUpdate(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(MapLayer::onTouchBegan, this); this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); bRet = true; } while (0); return bRet; }
void prep::testTMX() { Size visibleSize = Director::getInstance()->getVisibleSize(); //创建游戏 TMXTiledMap* tmx = TMXTiledMap::create("map.tmx"); tmx->setPosition(visibleSize.width / 2, visibleSize.height / 2); tmx->setAnchorPoint(Vec2(0.5, 0.5)); tmx->setName("paopaot"); this->addChild(tmx); //获取对象层 TMXObjectGroup* objects = tmx->getObjectGroup("bubble"); //对应对象层的对象数组 ValueVector container = objects->getObjects(); //遍历 for (auto obj : container) { ValueMap values = obj.asValueMap(); //获取坐标(cocos2dx坐标) int x = values.at("x").asInt(); int y = values.at("y").asInt(); auto buble = Sprite::create("bubble.png"); buble->setPosition(Vec2(x, y + 64)); buble->ignoreAnchorPointForPosition(true); tmx->addChild(buble, 2); prep::bubbles.pushBack(buble); } objects = tmx->getObjectGroup("wall"); container = objects->getObjects(); for (auto obj : container) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); auto wall = Sprite::create("wall.png"); wall->setPosition(Vec2(x, y + 64)); wall->ignoreAnchorPointForPosition(true); tmx->addChild(wall, 1); prep::walls.pushBack(wall); } objects = tmx->getObjectGroup("money"); container = objects->getObjects(); for (auto obj : container) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); auto goal = Sprite::create("money.png"); goal->setPosition(Vec2(x, y + 64)); goal->ignoreAnchorPointForPosition(true); tmx->addChild(goal, 1); prep::money.pushBack(goal); } objects = tmx->getObjectGroup("player"); container = objects->getObjects(); for (auto obj : container) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); auto player = Sprite::create("player.png"); player->setPosition(Vec2(x, y + 64)); player->ignoreAnchorPointForPosition(true); player->setName("player"); tmx->addChild(player, 1); } }