Search…

Làm Thế Nào Để Tạo Một HUD Layer với Cocos2d-x 3.x.x?

28/09/20202 min read
Giới thiệu về HUD, tác dụng của HUD trong dự án games sử dụng engine Cocos2d-x.

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

 

IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024