Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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:

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.

1 decade ago by lazer

The thing is, if you only show the health bar when the enemy is in the process of taking damage it seems like the health bar would only be visible for a moment (or even not at all, as it would flash up and disappear only while the receiveDamage function is being called, unless you have a timer set up for your "damageTaken" variable).

Or do you mean that you're trying to show the health bar continuously after the monster has already lost one or more health (which is what the name 'damageTaken' seems to imply, which is why I'm clarifying)?

I think it all depends on how the damageTaken variable is set up, but right now what's the problem you're having - are you just not seeing the health bar, or are you getting some sort of error with the code you have?

1 decade ago by Oscar

I actually fixed it - my fault that i didnt clarify. I wanted to show the health bar only when the zombie is below maxHealth. :-))
Page 1 of 1
« first « previous next › last »