1 decade ago by Oscar
Hello Forum.
I'm pretty new to impact, and im reading the book, that Dominic, referred to.
I am in the progress of building a zombie apocalypse pixel based RPG. Everything is going as it should, besides. I have one question about how to do so that my health bar only shows when entity/monster is taking damage.
So what i wanted to ask is, how to work out a little. While monster is losing health show health bar. When monster is not losing health, hide health bar.
Here is my code for the zombie:
I've already made some code for the damageTaken variables, and also done so if the damageTaken = true then show health bars. But the rest i can't really figure out.
I'm pretty new to impact, and im reading the book, that Dominic, referred to.
I am in the progress of building a zombie apocalypse pixel based RPG. Everything is going as it should, besides. I have one question about how to do so that my health bar only shows when entity/monster is taking damage.
So what i wanted to ask is, how to work out a little. While monster is losing health show health bar. When monster is not losing health, hide health bar.
Here is my code for the zombie:
ig.module ( 'game.entities.zombie' ) .requires ( 'impact.entity' ) .defines(function(){ EntityZombie = ig.Entity.extend({ animSheet: new ig.AnimationSheet( 'media/zombie.png', 16, 16 ), // stats. health: 190, maxHealth: 190, damageTaken: false, size: {x: 8, y:14}, offset: {x: 4, y: 2}, maxVel: {x: 100, y: 100}, flip: false, friction: {x: 150, y: 0}, speed: 8, // Player collesion. Husk B = Enemy. type: ig.Entity.TYPE.B, checkAgainst: ig.Entity.TYPE.A, collides: ig.Entity.COLLIDES.PASSIVE, init: function( x, y, settings ) { this.parent( x, y, settings ); this.addAnim( 'walk', 0.2, [0,1,2,3,4,5] ); }, update: function() { // return when near an edge. if( !ig.game.collisionMap.getTile( this.pos.x + (this.flip ? +4 : this.size.x -4), this.pos.y + this.size.y+1 ) ) { this.flip = !this.flip; } var xdir = this.flip ? -1 : 1; this.vel.x = this.speed * xdir; this.currentAnim.flip.x = this.flip; this.parent(); }, handleMovementTrace: function( res ) { this.parent( res ); // Collision with wall, flip. if( res.collision.x ) { this.flip = !this.flip; } }, // mist liv ved at collidere med zombie check: function( other ) { other.receiveDamage( 0.8, this ); }, draw: function() { if (this.damageTaken == true){ // ramme om health bar ig.system.context.fillStyle = "rgb(0,0,0)"; ig.system.context.beginPath(); ig.system.context.rect( (this.pos.x - ig.game.screen.x) * ig.system.scale, (this.pos.y - ig.game.screen.y - 8) * ig.system.scale, this.size.x * ig.system.scale, 4 * ig.system.scale ); ig.system.context.closePath(); ig.system.context.fill(); // health bar ig.system.context.fillStyle = "rgb(255,0,0)"; ig.system.context.beginPath(); ig.system.context.rect( (this.pos.x - ig.game.screen.x + 1) * ig.system.scale, (this.pos.y - ig.game.screen.y - 7) * ig.system.scale, ((this.size.x - 2) * (this.health / this.maxHealth)) * ig.system.scale, 2 * ig.system.scale ); ig.system.context.closePath(); ig.system.context.fill(); } this.parent(); } }); });
I've already made some code for the damageTaken variables, and also done so if the damageTaken = true then show health bars. But the rest i can't really figure out.