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 gắn thêm các button vào game, và hiện thực hàm resetButton cho game.
Hiện thực
Hình ảnh có được sau khi hoàn thành bài viết này:
![ss_1](https://resources.stdio.vn/content/article/5ef6226724fd2869e91f1926/resources/res-1601193868-1601193868104.png)
Để có thể hiện thực bên dưới, thêm vài định nghĩa như sau ở file config.cpp
static const char *PATH_SPR_BTN_RIGHT = "spr_btn_right.png"; static const char *PATH_SPR_BTN_RIGHT_PRESS = "spr_btn_right_press.png"; static const char *PATH_SPR_BTN_WRONG = "spr_btn_wrong.png"; static const char *PATH_SPR_BTN_WRONG_PRESS = "spr_btn_wrong_press.png"; enum { BTN_NONE = 0, BTN_RIGHT, BTN_WRONG };
Tại GameScene.h
Thêm vào các biến _btnRight
, _btnWrong
, _clickedButton
, hàm resetButton
như sau:
#ifndef __GAME_SCENE_H__ #define __GAME_SCENE_H__ #include "cocos2d.h" #include "ui/CocosGUI.h" USING_NS_CC; class GameScene : 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(); void update(float); void generateNextChallenge(); void resetButtons(); // implement the "static create()" method manually CREATE_FUNC(GameScene); private: Layer *_layerPresentation; ProgressTimer *_timer; int _nLeftSuits; int _nRightSuits; int _clickedButton; Vec2 _centerSuitPostionLeft; Vec2 _centerSuitPostionRight; float _currentTime; float _maxTimer; cocos2d::ui::Button *_btnRight; cocos2d::ui::Button *_btnWrong; Size visibleSize; Vec2 origin; }; #endif // __GAME_SCENE_H__
Lưu ý: Nhớ là include "ui/CocosGUI.h"
vào đầu file.
Tại GameScene.cpp
Trong hàm init
Khởi tạo giá trị cho các biến mới như sau:
_clickedButton = BTN_NONE; _btnRight = _btnWrong = nullptr;
Gọi hàm resetButton (dòng này phải nằm dưới 2 dòng ở trên):
resetButtons();
Hiện thực hàm resetButton
Hàm này có nhiệm vụ gắn vào 2 button đúng và sai trong game Zero.
void GameScene::resetButtons() { if (_btnRight != nullptr) { this->removeChild(_btnRight, true); } if (_btnWrong != nullptr) { this->removeChild(_btnWrong, true); } _btnRight = ui::Button::create(PATH_SPR_BTN_RIGHT, PATH_SPR_BTN_RIGHT_PRESS, PATH_SPR_BTN_RIGHT, ui::TextureResType::PLIST); _btnRight->setPosition(PositionManager::getInstance()->_right_btn); _btnRight->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type){ switch (type) { case ui::Widget::TouchEventType::BEGAN: break; case ui::Widget::TouchEventType::ENDED: _clickedButton = BTN_RIGHT; break; } }); this->addChild(_btnRight, zUI); _btnWrong = ui::Button::create(PATH_SPR_BTN_WRONG, PATH_SPR_BTN_WRONG_PRESS, PATH_SPR_BTN_WRONG, ui::TextureResType::PLIST); _btnWrong->setPosition(PositionManager::getInstance()->_wrong_btn); _btnWrong->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type){ switch (type) { case ui::Widget::TouchEventType::BEGAN: break; case ui::Widget::TouchEventType::ENDED: _clickedButton = BTN_WRONG; break; } }); this->addChild(_btnWrong, zUI); }
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ố