Quantcast
Channel: 2D Latest Topics
Viewing all articles
Browse latest Browse all 623

Player health bar problem

$
0
0

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

 


Viewing all articles
Browse latest Browse all 623

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>