bool CityScene::init() { Layer::init(); char szRound[260]; sprintf(szRound, "Round%d.tmx", mRound); TileMapInfo* tileMapInfo = TileMapInfo::createMapInfoWithFile(szRound); TMXTiledMap* tmxTileMap = tileMapInfo->getTileMap(); this->addChild(tmxTileMap); mPlayerTank[0] = Tank::createTankWithTankType("player2U.png", tileMapInfo); mPlayerTank[0]->setObjType(PlayerTank); mPlayerTank[0]->getBullet(); Size tileSize = tmxTileMap->getTileSize(); Size mapSize = tmxTileMap->getContentSize(); mPlayerTank[0]->setPosition(Point(mapSize.width / 2 - tileSize.width * 3, tileSize.height)); // mLayerPanel = Panel::create(); addChild(mLayerPanel, 3); // mEnemyAI = EnemyAI::createEnemyAIWithTank(mPlayerTank[0]); return true; }
//------------------------------------------------------------------ // // TMXOrthoTest // //------------------------------------------------------------------ TMXOrthoTest::TMXOrthoTest() { // // Test orthogonal with 3d camera and anti-alias textures // // it should not flicker. No artifacts should appear // //CCLayerColor* color = LayerColor::create( ccc4(64,64,64,255) ); //addChild(color, -1); TMXTiledMap* map = TMXTiledMap::create("TileMaps/orthogonal-test2.tmx"); addChild(map, 0, kTagTileMap); Size CC_UNUSED s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); Array * pChildrenArray = map->getChildren(); SpriteBatchNode* child = NULL; Object* pObject = NULL; CCARRAY_FOREACH(pChildrenArray, pObject) { child = (SpriteBatchNode*)pObject; if(!child) break; child->getTexture()->setAntiAliasTexParameters(); }
void AttackMapLayer::addMapMenu(){ //以下全部为设置地图的窗口 TMXTiledMap * changeMap = TMXTiledMap::create("bakground(1).tmx"); this->addChild(changeMap); float cmX = (vSize.width - changeMap->getContentSize().width) / 2; changeMap->setPosition(Vec2(cmX, 0)); TMXObjectGroup * objectgroup = changeMap->getObjectGroup("object"); ValueMap valuemap = objectgroup->getObject("object1"); float x = valuemap.at("x").asFloat(); float y = valuemap.at("y").asFloat(); auto menuitem = MenuItemImage::create("stage-9.png", "stage-9.png", CC_CALLBACK_1(AttackMapLayer::changeMap_a_1, this)); menuitem->setPosition(Vec2(x, y)); auto menu = Menu::create(menuitem, NULL); menu->setPosition(Vec2(cmX, 0)); //房后闪光 auto flashBG = Sprite::create("crusade_stage_7_curren.png"); flashBG->setPosition(Vec2(x , y)); //动画渐入渐出 auto flashAnimationOut = FadeOut::create(0.5); auto flashAnimationIn = FadeIn::create(0.5); auto seqAction = Sequence::create(flashAnimationOut, flashAnimationIn, NULL); flashBG->runAction(RepeatForever::create(seqAction)); addChild(flashBG); valuemap = objectgroup->getObject("object2"); x = valuemap.at("x").asFloat(); y = valuemap.at("y").asFloat(); auto menuitem1 = MenuItemImage::create("stage-19.png", "stage-19.png", CC_CALLBACK_1(AttackMapLayer::changeMap_a_2, this)); menuitem1->setPosition(Vec2(x, y)); menu->addChild(menuitem1); valuemap = objectgroup->getObject("object3"); x = valuemap.at("x").asFloat(); y = valuemap.at("y").asFloat(); auto menuitem2 = MenuItemImage::create("stage-23.png", "stage-23.png", CC_CALLBACK_1(AttackMapLayer::changeMap_a_3, this)); menuitem2->setPosition(Vec2(x, y)); menu->addChild(menuitem2); valuemap = objectgroup->getObject("object4"); x = valuemap.at("x").asFloat(); y = valuemap.at("y").asFloat(); auto menuitem3 = MenuItemImage::create("stage-25.png", "stage-25.png", CC_CALLBACK_1(AttackMapLayer::changeMap_a_4, this)); menuitem3->setPosition(Vec2(x, y)); menu->addChild(menuitem3); this->addChild(menu); }
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; }
bool CityScene::init() { if (!Layer::init()) { return false; } char szRound[260]; sprintf(szRound, "Round%d.tmx", mRound); TileMapInfo* tileMapInfo = TileMapInfo::createMapInfoWithFile(szRound); TMXTiledMap* tmxTileMap = tileMapInfo->getTileMap(); this->addChild(tmxTileMap); mPlayerTank[0] = Tank::createTankWithTankType("player2U.png", tileMapInfo); Size tileSize = tmxTileMap->getTileSize(); Size mapSize = tmxTileMap->getContentSize(); mPlayerTank[0]->setPosition(Vec2(mapSize.width / 2 - tileSize.width * 3, tileSize.height)); mLayerPanel = Panel::create(); addChild(mLayerPanel, 3); return true; }
Node* GameMapReader::createNodeWithFlatBuffers(const flatbuffers::Table *gameMapOptions) { TMXTiledMap* tmx = nullptr; auto options = (GameMapOptions*)gameMapOptions; auto fileNameData = options->fileNameData(); bool fileExist = false; std::string errorFilePath = ""; std::string path = fileNameData->path()->c_str(); int resourceType = fileNameData->resourceType(); switch (resourceType) { case 0: { if (FileUtils::getInstance()->isFileExist(path)) { fileExist = true; } else { errorFilePath = path; fileExist = false; } break; } default: break; } if (fileExist) { /* Whether tileset is valid. */ auto mapInfo = TMXMapInfo::create(path); auto& layers = mapInfo->getLayers(); bool valid = false; std::string layerName = ""; for (const auto &layerInfo : layers) { valid = false; if (layerInfo->_visible) { Size size = layerInfo->_layerSize; auto& tilesets = mapInfo->getTilesets(); if (tilesets.size()>0) { TMXTilesetInfo* tileset = nullptr; for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter) { tileset = *iter; if (tileset) { for( int y=0; y < size.height; y++ ) { for( int x=0; x < size.width; x++ ) { int pos = static_cast<int>(x + size.width * y); int gid = layerInfo->_tiles[ pos ]; if( gid != 0 ) { if( (gid & kTMXFlippedMask) >= tileset->_firstGid ) { valid = true; break; } } } if (valid) { break; } } } } } if (!valid) { layerName = layerInfo->_name; break; } } else { valid = true; } } if (!valid) { Node* node = Node::create(); setPropsWithFlatBuffers(node, (Table*)gameMapOptions); auto label = Label::create(); label->setString(__String::createWithFormat("Some error of gid are in TMX Layer '%s'", layerName.c_str())->getCString()); node->setScale(1.0f); node->addChild(label); return node; } /**/ tmx = TMXTiledMap::create(path); if (tmx) { //prevent that editor's data does not match in size and resources Size fileSize = tmx->getContentSize(); setPropsWithFlatBuffers(tmx, (Table*)gameMapOptions); tmx->setContentSize(fileSize); } } else { Node* node = Node::create(); setPropsWithFlatBuffers(node, (Table*)gameMapOptions); return node; } return tmx; }
PushBoxScene::PushBoxScene() { TMXTiledMap* mytmx = TMXTiledMap::create("Pushbox/map.tmx"); mytmx->setPosition(visibleSize.width / 2, visibleSize.height / 2); mytmx->setAnchorPoint(Vec2(0,0)); myx = (visibleSize.width - mytmx->getContentSize().width) / 2; myy = (visibleSize.height - mytmx->getContentSize().height) / 2; this->addChild(mytmx, 0); count = 0; success = 0; /*mon = Sprite::create("Pushbox/player.png"); mon->setAnchorPoint(Vec2(0, 0)); mon->setPosition(Vec2(SIZE_BLOCK*1+myx, SIZE_BLOCK*8+myy)); mon->setTag(TAG_PLAYER); this->addChild(mon, 1);*/ TMXObjectGroup* objects = mytmx->getObjectGroup("wall"); //从对象层中获取对象数组 ValueVector container = objects->getObjects(); //遍历对象 for (auto obj : container) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); Sprite* temp = Sprite::create("Pushbox/wall.png"); temp->setAnchorPoint(Point(0, 0)); temp->setPosition(Point(x,y+64)); mywall.pushBack(temp); this->addChild(temp, 1); } TMXObjectGroup* objects1 = mytmx->getObjectGroup("box"); //从对象层中获取对象数组 ValueVector container1 = objects1->getObjects(); //遍历对象 for (auto obj : container1) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); Sprite* temp = Sprite::create("Pushbox/box.png"); temp->setAnchorPoint(Point(0, 0)); temp->setPosition(Point(x, y+64)); mybox.pushBack(temp); this->addChild(temp, 3); } TMXObjectGroup* objects2 = mytmx->getObjectGroup("player"); //从对象层中获取对象数组 ValueVector container2 = objects2->getObjects(); //遍历对象 for (auto obj : container2) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); Sprite* temp = Sprite::create("Pushbox/player.png"); temp->setAnchorPoint(Point(0, 0)); temp->setPosition(Point(x, y+64)); mon = temp; this->addChild(temp, 2); } TMXObjectGroup* objects3 = mytmx->getObjectGroup("goal"); //从对象层中获取对象数组 ValueVector container3 = objects3->getObjects(); //遍历对象 for (auto obj : container3) { ValueMap values = obj.asValueMap(); int x = values.at("x").asInt(); int y = values.at("y").asInt(); Sprite* temp = Sprite::create("Pushbox/goal.png"); temp->setAnchorPoint(Point(0, 0)); temp->setPosition(Point(x, y+64)); mygoal.pushBack(temp); this->addChild(temp, 1); } }