Hướng dẫn hiện thực lại game ZERO 1 cách đơn giản. Giúp làm quen cách để hoàn thành 1 game, cũng như cách tổ chức project trong game.
Hướng dẫn cách tạo kích thước màn hình trong game, hiện thực lại position manager để quản lý các đối tượng trong game.
Hiện thực
Trước khi hiện thực, tạo thêm một file config.h. Đây sẽ là nơi để lưu trữ toàn bộ các definition cũng như các constants trong game.
Dưới đây là 1 số constants cần thiết trong bài viết này.
#ifndef __CONFIG_H__ #define __CONFIG_H__ static const float SCREEN_DESIGN_WIDTH = 1080.0f; static const float SCREEN_DESIGN_HEIGHT = 1920.0f; static const char *PATH_CONF_POS = "conf_objects_pos.plist"; #endif // __CONFIG_H__
Ở đây chọn kích thước màn hình là 1080x1920
.
Thiết kế lại kích thước màn hình
Để thiết kế lại kích thước màn hình cho game. Vào AppDelegate.cpp, trong hàm applicationDidFinishLaunching
thêm 1 vài đoạn code như sau:
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewImpl::create("ZERO"); director->setOpenGLView(glview); glview->setFrameSize(SCREEN_DESIGN_WIDTH * 0.3, SCREEN_DESIGN_HEIGHT * 0.3); } glview->setDesignResolutionSize(SCREEN_DESIGN_WIDTH, SCREEN_DESIGN_HEIGHT, ResolutionPolicy::NO_BORDER); // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object auto scene = LoadScene::createScene(); // run director->runWithScene(scene); return true; }
Ở đoạn code trên có 2 dòng code thêm vào đó là dòng 8 và dòng 11.
- Dòng 8: kích thước màn hình trong game.
- Dòng 11: set lại kích thước resource trong game theo kích thước màn hình truyền vào, ở đây là 1080x1920. Khi thay đổi lại kích thước màn hình thì cocos2d-x sẽ hỗ trợ resource tự canh theo. Làm cho hình ảnh không bị nhỏ lại khi build qua màn hình lớn hơn và ngược lại.
Position manager
Sử dụng position manager.
File PositionManager.h
#ifndef __POSITION_MANAGER_H__ #define __POSITION_MANAGER_H__ #include "cocos2d.h" USING_NS_CC; class PositionManager { public: PositionManager(); ~PositionManager(); static PositionManager* getInstance(); void loadObjectsPosition(const char* pListPath); Vec2 getObjectPosition(const String* object_position); Vec2 _title; Vec2 _play_btn; Vec2 _board; private: static PositionManager* _instance; }; #endif // __POSITION_MANAGER_H__
File PositionManager.cpp
#include "cocos2d.h" #include <vector> #include "Config.h" #include "PositionManager.h" USING_NS_CC; PositionManager::PositionManager() { } PositionManager::~PositionManager() { } PositionManager* PositionManager::_instance = 0; PositionManager* PositionManager::getInstance() { if (!_instance) _instance = new PositionManager(); return _instance; } void PositionManager::loadObjectsPosition(const char* pListPath) { Dictionary* object_list_position = Dictionary::createWithContentsOfFile(pListPath); // get board position const String* board_pos_str = object_list_position->valueForKey("board"); PositionManager::getInstance()->_board = getObjectPosition(board_pos_str); // get play button position const String* play_btn_pos_str = object_list_position->valueForKey("play_button"); PositionManager::getInstance()->_play_btn = getObjectPosition(play_btn_pos_str); // get title position const String* title_pos_str = object_list_position->valueForKey("title"); PositionManager::getInstance()->_title = getObjectPosition(title_pos_str); } Vec2 PositionManager::getObjectPosition(const String* object_position) { Vec2 position; int value = 0; for (size_t i = 0; i < object_position->_string.length(); i++) { if (object_position->_string[i] != ',') { value *= 10; value += object_position->_string[i] - '0'; } else { position.x = value; value = 0; } } position.y = value; return position; }
Lưu ý: Trong PositionManager trên chỉ mới hiện thực 3 đối tượng. Khi cần làm sẽ thêm vào các đối tượng khác nữa.
Vào file LoadScene.cpp, trong hàm update thêm vào đoạn code mới để được như sau:
void LoadScene::update(float dt) { switch (m_loadingStep) { // Load sprite case 0: SpriteFrameCache::getInstance()->addSpriteFramesWithFile("spr_sheet_zero.plist", "spr_sheet_zero.png"); break; // Load position case 1: PositionManager::getInstance()->loadObjectsPosition(PATH_CONF_POS); break; // Load sounds case 2: break; case 3: Director::getInstance()->replaceScene(MainMenuScene::createScene()); break; } m_loadingStep++; }
Download
Bài chung series
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 1: Tạo Scene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 2: Hiện Thực LoadScene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 3: Thiết Kế Kích Thước Màn Hình, Quản Lý Đối Tượng
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 4: Hiện thực MainMenuScene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 5: Hiện thực GameScene - Vẽ Suit
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 6: Hiện thực GameScene - Thêm thời gian
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 7: Hiện thực GameScene - Button
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 8: Hiện thực GameScene - Xử lý sự kiện trong game
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 9: Hiện thực GameScene - Điểm Số