Jump to content

Player health bar problem


soulVampire
 Share

Recommended Posts

I have a healthBar entity given by the code below which is in a separate file called health_bar_entity.js(melonjs code base)

game.HealthBar = me.Renderable.extend(
{
    init: function(hitPoints, maxHitPoints)
    {     
        this._super(me.Renderable, "init", [0, -24, 48, 6]);
        this.z = 100;
        this.hitPoints = hitPoints;
        this.maxHitPoints = maxHitPoints;
        this.health = 1;
        this.currentHealth = 1;
        this.setHealth(hitPoints);
        this.borderColor = '#000';
        this.alwaysUpdate = false;
    },
    
    draw : function(renderer) 
    {
       if(this.health>=0)
       {
           renderer.setColor(this.borderColor);
           renderer.fillRect(this.pos.x, this.pos.y, this.width, this.height);
           renderer.setColor('#F00');
           renderer.fillRect(this.pos.x+1, this.pos.y+1, this.width-2, this.height-2);
           renderer.setColor('#0F0');
           if(this.health>1)
              renderer.fillRect(this.pos.x+1, this.pos.y+1, this.width-2, this.height-2);
           else
              renderer.fillRect(this.pos.x+1, this.pos.y+1, (this.width-2)*this.health, this.height-2);
       }
    },
    
    update : function() 
    {
       if(this.updateOnce)
       {
           this.updateOnce = false;
           return true;
       }
       if(this.health==this.currentHealth)
           return false;
       this.health = this.currentHealth;
           return true;
    },
    
    setHealth: function(hitPoints)
    {
       this.currentHealth = hitPoints/this.maxHitPoints;
    },
    
    setCurrentAnimation: function()
    {
    //do nothing
    },
    
    setAnimationFrame: function(blarg)
    {
    //do nothing
    }
});

and I want to add this to the player entity, but when I try to add to the player by using the code below, this is in the file game_player_entity.js(melonjs code base)

    this.player_health = 50;
    this.player_max_health = 100;
    this.healthBar = me.pool.pull("playerHealthbar", this.player_health, 
    this.player_max_health);
    console.log('health bar' + this.healthBar);

this.addChild(this.healthbar, 9);

it give me an error don't know whats going wrong have looked at the api and it seems that it should work

 

Edited by soulVampire
Refresh description
Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...