Chuỗi bài viết hướng dẫn hiện thực lại game ZERO đơ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.
Bài viết này sẽ hướng dẫn cách tạo scene cho game trong Cocos2d-x.
Tạo project
Để có thể viết game, công việc đầu tiên là tạo mới một project.
Sau khi tạo xong, vào project mở solution (win32) lên.
Tạo scene
Sau khi tạo một project, cocos2d-x sẽ tự động tạo ra 4 file:
- AppDelegate.h
- AppDelegate.cpp
- HelloWorldScene.h
- HelloWorldScene.cpp.
Trong đó với HelloWorldScene
là scene mặc định.
Tiếp theo là hướng dẫn cách để tạo các scene mới trong game từ HelloWorldScene
. Các scene cần tạo trong game này là:
- LoadScene
- MainMenuScene
- GameScene
- GameOverScene.
Đổi tên 2 file HelloWorldScene.h và HelloWorldScene.cpp ở trên lần lượt thành LoadScene.h và LoadScene.cpp.
Trong file LoadScene.h đổi các từ khóa HelloWordScene
thành LoadScene
được như sau:
#ifndef __LOAD_SCENE_H__ #define __LOAD_SCENE_H__ #include "cocos2d.h" class LoadScene : 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(LoadScene); }; #endif // __LOAD_SCENE_H__
Tương tự trong file LoadScene.cpp
#include "LoadScene.h" USING_NS_CC; Scene* LoadScene::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = LoadScene::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 LoadScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); return true; }
Trong file AppDelegate.cpp thay dòng
#include "HelloWorldScene.h"
thành
#include "LoadScene.h"
và dòng
auto scene = HelloWorld::createScene();
thành
auto scene = LoadScene::createScene();
Tiếp theo vào thư mục classes của project.
Từ 2 file LoadScene.h và LoadScene.cpp có sẵn, tiến hành copy ra và đổi tên thành các scene khác như sau:
Kéo thả các scene vừa tạo vào môi trường làm việc được như hình sau:
Vào từng scene vừa tạo và tiến hành đổi nội dung tương tự như với LoadScene.
Download source code
Dưới đây là source code sau khi kết thúc bài viết này.
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ố