DATA_OLD/Cocos2d-x 기능성 게임 개발과정
[D_016] Tiled를 이용한 맵제작 / cocos 적용 / 이동, 타일 속성 예제
웽웽
2014. 12. 2. 16:32
저장한 맵을 cocos프로젝트의 resources폴더에 위치시킴.
+ 맵타일의 ID는 0번부터 시작이지만, cocos에서는 1번부터 시작한다. 그러니까 이 두개를 잘 맞춰줘야함.
예제 코드
맵 파일 내용 뽑아내는 기술이 많으므로 필요할때마다 보고 복습할것
캐릭터 이동 / 끝에 도달 시 맵 이동 / 타일 ID및 속성값 추출 등
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); Size winSize; float bgX; virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event); }; #endif // __HELLOWORLD_SCENE_H__ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } winSize = Director::getInstance()->getWinSize(); bgX = 0; auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); auto map = TMXTiledMap::create("map01.tmx"); map->setTag(2); this->addChild(map); //오브젝트를 가져오기 위한 오브젝트 그룹 추가 auto objectGroup = map->getObjectGroup("Object Layer 1"); //해당 오브젝트 그룹 내에 있는 오브젝트 가져옴 auto objectStart = objectGroup->getObject("start"); float x = objectStart["x"].asFloat(); float y = objectStart["y"].asFloat(); auto man = Sprite::create("kongman.png"); man->setTag(1); man->setPosition(Vec2(x, y)); this->addChild(man); return true; } void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { auto spr = (Sprite*)this->getChildByTag(1); Vec2 pos = spr->getPosition(); auto map = (TMXTiledMap*)this->getChildByTag(2); switch (keyCode) { case EventKeyboard::KeyCode::KEY_LEFT_ARROW: spr->setPosition(pos.x - 32, pos.y); break; case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: spr->setPosition(pos.x + 32, pos.y); break; case EventKeyboard::KeyCode::KEY_UP_ARROW: spr->setPosition(pos.x, pos.y+32); break; case EventKeyboard::KeyCode::KEY_DOWN_ARROW: spr->setPosition(pos.x, pos.y-32); break; default: break; } if (spr->getPositionX() < 16) { spr->setPosition(Vec2(16, spr->getPositionY())); bgX += 32; if (bgX > 0) bgX = 0; } else if (spr->getPositionX() > winSize.width - 16) { //스프라이트를 원위치로 옮겨주고 spr->setPosition(Vec2(winSize.width - 16, spr->getPositionY())); //스프라이트 대신 배경을 움직임 bgX -= 32; //만약 배경위치가 범위밖이면 범위안으로 옮겨줌 if (bgX < -(3200 - 480)) bgX = -(3200 - 480); } else if (spr->getPositionY() < 16) { spr->setPosition(Vec2(spr->getPositionX(), 16)); } else if (spr->getPositionY() > winSize.height - 16) { spr->setPosition(Vec2(spr->getPositionX(), winSize.height - 16)); } //보정된 지도 위치에 따라 지도위치를 옮겨줌 map->setPosition(Vec2(bgX, 0)); int indexX = spr->getPositionX() / 32; int indexY = (10 - spr->getPositionY() / 32); auto tileLayer = map->getLayer("Tile Layer 1"); int GID = tileLayer->getTileGIDAt(Vec2(indexX, indexY)); CCLOG("GID : %d", GID); if (GID == 31) { // 해당 GID에 저장되어 있는 속성 데이터를 가져옴 ValueMap property = map->getPropertiesForGID(31).asValueMap(); // 가져온 속성값 중에서 특정값에 해당되는 내용을 가져옴 Value wall = property.at("WALL"); // 저장된 내용과 비교 if (wall.asString().compare("NO")) { CCLOG("WALL : YES"); } } } |
파일