1 decade ago by Bushstar
I've created a health bar entity which I plan to attach to the player and enemies. The problem is that I cannot get the health bar to stick where it should be. All the health bars seem to float around more than the parent moves. So the health bar travels twice as far as things move. All the health bars shift around as the player moves about. I do not know why as the images are supposed to be drawn in relation to the entities that spawned the health bar.
This is my code. You can see that I pass the player or enemy object to the health bar so it can track it and check its health and maximum health.
You can see the problem in action on my dev page.
http://project.dnsalias.com/
This is my code. You can see that I pass the player or enemy object to the health bar so it can track it and check its health and maximum health.
ig.module( 'game.entities.health' ) .requires( 'impact.entity' ) .defines(function(){ EntityHealth = ig.Entity.extend({ healthImg: new ig.Image('media/healthbar.png'), entityObj: null, init: function(x, y, settings) { this.entityObj = settings.entityObj; this.parent(x, y); }, draw: function() { this.parent(); var currentHealth = Math.floor((this.entityObj.health/this.entityObj.maxHealth) * 31); this.healthImg.draw(this.entityObj.pos.x, this.entityObj.pos.y, 0, 0, 1, 5); for (var i = 1; i < currentHealth; i++) { this.healthImg.draw(this.entityObj.pos.x + i, this.entityObj.pos.y, 1, 0, 1, 5); } for (var i = currentHealth; i < 31; i++) { this.healthImg.draw(this.entityObj.pos.x + i, this.entityObj.pos.y, 2, 0, 1, 5); } this.healthImg.draw(this.entityObj.pos.x + 31, this.entityObj.pos.y, 0, 0, 1, 5); } }) });
You can see the problem in action on my dev page.
http://project.dnsalias.com/