HUD là gì?
Là thanh trạng thái để chứa và hiển thị các thông tin dữ liệu cho người chơi.
HUD chứa đựng những gì?
Các thông tin được hiển thị trên HUD phụ thuộc vào rất nhiều vào game, có rất nhiều thông tin và tính năng trên HUD mà người chơi tiếp thu được trên nhiều game khác nhau Một số tính năng phổ biến bao gồm:
- Health/Live
- Times
- Weapons/ammunition
- Menus
- Mini-map
- Game progression
- Speedomte
- ...
Ví dụ
Hãy nghĩ rằng HUD như là một Layer khác trong Scene, với mục đích là chứa các tính năng cần trong dự án games. Dưới đây hiện thực một HUD đơn giản.
Header:
// HUD.h #ifndef __HUD_h__ #define __HUD_h__ #include "cocos2d.h" class HUD: public cocos2d::LayerColor { public: HUD(); virtual ~HUD(); HUD* createLayer(const std::string& _message); void initOptions(const std::string& _message); virtual void draw (cocos2d::Renderer * renderer, const cocos2d::Mat4 & transform, bool transformUpdated); inline virtual cocos2d::LabelBMFont* getMessageLabel() { return _messageLabel; }; protected: private: cocos2d::LabelBMFont* _messageLabel; }; #endif // __HUD_h__
Soucre:
#include "HUD.hpp" HUD::HUD() {} HUD::~HUD() {} HUD* HUD::createLayer(const std::string& _message) { HUD* hudMessage = new MessageHUD(); hudMessage->create(); hudMessage->setColor(cocos2d::Color3B(0, 0, 0)); hudMessage->setContentSize(cocos2d::Size::Size(1, 1)); hudMessage->setAnchorPoint(cocos2d::Vec2(0, 0)); hudMessage->initOptions(_message); return hudMessage; } void HUD::initOptions(const std::string& _message) { _messageLabel = cocos2d::LabelBMFont::create(_message.c_str(), "Marker Felt Small.fnt"); _messageLabel->setColor(cocos2d::Color3B(255, 215, 0)); addChild(_messageLabel, 1); _messageLabel->setPosition(cocos2d::Vec2(Director::getInstance()->getVisibleSize().width, Director::getInstance()->getVisibleSize().height)); } void MessageHUD::draw(cocos2d::Renderer * renderer, const cocos2d::Mat4 & transform, bool transformUpdated) {}
Khởi tạo trong project:
HUD* _hudMessage = HUD::createLayer("Hello Stdio.vn"); this->addChild(_hudMessage, 2);
HUD ở trên có tác dụng tạo một dòng tìn nhắn hiển thị cho người chơi.
Tổng kết
- Ngoài việc hiển thị các thông tin cho người chơi, HUD có thể chứa các Button,... hoặc thậm chí là một Scene.
- HUD có thể không cần thiết nhưng nếu lượng thông tin và tính năng cần hiển thị trong game quá lớn hoặc là muốn source code của project dễ sửa lỗi, thêm tính năng và bảo trì thì HUD là một lựa chọn phù hợp.
Tham khảo
https://www.cocos2d-x.org