원글 출처 : http://horns.tistory.com/5
-2- 기본 화면 구성
1. 화면을 세로로 고정하기
/proj.android/AndroidManifest.xml에서 20번째 줄의 screenOrientation을 portrait로 수정한다.
18 19 20 21 22 | <activity android:name="org.cocos2dx.cpp.AppActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="orientation"> |
2. 배경 이미지 삽입
1) 시작하기
불필요한 코드를 삭제하고 아래와 같이 시작한다.
/Classes/HelloWorldScene.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::Layer { public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__ |
이걸 하지 않으면 헤더파일 내에서 cocos2d 클래스랑 연결이 되지 않음.
/Classes/HelloWorldScene.cpp
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 | #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; } return true; } |
2) 배경 이미지 삽입
*게임에 사용된 이미지는 원출처(http://horns.tistory.com/5)에 있으므로 여기에 추가로 업로드 하지 않는다.
HelloWorldScene.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 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::Layer { private: Size _screenSize; public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); void createGameScene(void); }; #endif // __HELLOWORLD_SCENE_H__ |
HelloWorldScene.cpp에서 배경 이미지를 생성한다.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } createGameScene(); return true; } void HelloWorld::createGameScene(){ _screenSize = Director::sharedDirector()->getWinSize(); //배경 이미지 삽입 Sprite* background = Sprite::create("game_back.png"); background->setPosition(ccp(_screenSize.width/2, _screenSize.height/2)); background->setScale(_screenSize.width/background->getContentSize().width); //화면에 꽉차게 배경화면 띄우기 this->addChild(background); } |
2) 나무 및 적 이미지 삽입
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 | void HelloWorld::createGameScene() { _screenSize = Director::sharedDirector()->getWinSize(); //배경 이미지 삽입 Sprite* background = Sprite::create("game_back.png"); background->setPosition(ccp(_screenSize.width / 2, _screenSize.height / 2)); background->setScale(_screenSize.width / background->getContentSize().width); //화면에 꽉차게 배경화면 띄우기 this->addChild(background); //나무 이미지 삽입 Sprite* tree = Sprite::create("tree_01.png"); tree->setPosition(ccp(_screenSize.width * 0.2f, _screenSize.height * 0.8f)); this->addChild(tree); tree = Sprite::create("tree_02.png"); tree->setPosition(ccp(_screenSize.width * 0.35f, _screenSize.height * 0.8f)); this->addChild(tree); tree = Sprite::create("tree_03.png"); tree->setPosition(ccp(_screenSize.width * 0.8f, _screenSize.height * 0.8f)); this->addChild(tree); //적 이미지 추가 Sprite* enemy = Sprite::create("enemy_01.png"); enemy->setPosition(ccp(_screenSize.width * 0.7f, _screenSize.height * 0.8f)); this->addChild(enemy); } |
Ctrl+B버튼을 눌러서 빌드해준 뒤 에러가 없는지 확인하고 실행해본다.
에러가 발생한다면 오타가 있는지 확인할 것.
실행 화면:
----------------------------------------------------------------------------------------------------------------
소스 삽입기가 라인넘버까지 지정이 가능하면 좋을텐데...뭔가 다른 편한걸 찾아봐야겠네요.