LifeHud.cpp

LifeHUD::LifeHud(int x, int y, int width, int height) : DisplayObject(x, y, width, height) {

}

LifeHUD::~LifeHud(void) {

} bool LifeHUD::Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx) { //draw code return true; }

void
LifeHUD::SetHealth(int health) { if(health < _health) { this->dispatchEvent(Event("HEALTH_REMOVED", this)); _health = health; } if(health > _health) { this->dispatchEvent(Event("HEALTH_ADDED", this)); _health = health; } if(health < 0 ) { this->dispatchEvent(Event("DEATH", this)); _health = 0; } }

Register Events:

LifeHUD* lifeHud = new LifeHUD();

lifeHud->addEventListener("HEALTH_ADDED", std::bind(&App::ON_HEALTH_ADDED_lifeHud, this, std::placeholders::_1));
lifeHud->addEventListener("HEALTH_REMOVED", std::bind(&App::ON_HEATLTH_REMOVED_lifeHud, this, std::placeholders::_1));
lifeHud->addEventListener("DEATH", std::bind(&App::ON_DEATH_lifeHud, this, std::placeholders::_1));

lifeHud->SetHealth(140);
Catch Event:
void App::ON_HEALTH_REMOVED_lifeHud(const Event &event) {
     LifeHUD* lifeHud= (LifeHUD*)event.data;

     //do something
}

void
App::ON_HEALTH_ADDED_lifeHud(const Event &event) { LifeHUD* lifeHud= (LifeHUD*)event.data; //do something }

void
App::ON_DEATH_lifeHud(const Event &event) { LifeHUD* lifeHud= (LifeHUD*)event.data; //do something }

Last edited Mar 27, 2013 at 11:13 PM by TypeOverride, version 7

Comments

No comments yet.