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 sebastianvivaldi

Hi, first of all I'm an newbie programmer and would be extraordinary if someone could help me. I'm trying to make my character take an item and have 10 seconds of invulnerability (like Mario and the star). I think it has to do with timers and cancel damage to player but I can not make it work. Any idea how to do this.

I have two entities Player and Star.

I would appreciate your help. Thanks and sorry for my english.

1 decade ago by Arantor

You're right, it's going to involve timers and cancelling damage to the player. I haven't tested the following but see no obvious reason why it shouldn't work.

Step 1: allow for the player to be invulnerable.

In the player entity, at the top where you declare its size and animations etc, add new properties:
invulnTimer: false,
invulnTime: 10,

(Technically we don't need to declare these up front, but it makes things more elegant if we do, especially the latter which keeps everything making sense)

In the player entity, also make sure to add this code:
	receiveDamage: function( amount, from ) {
		if (!this.invulnTimer || this.invulnTimer.delta() < 0) {
			this.parent(amount, from);
			this.invulnTimer = false;
		}
	},

What we're saying here is that if the timer is false (not active) or it's run out (delta less than 0 as per the ig.Timer documentation), call the main damage function to handle damage, but otherwise do nothing.

This means all the normal logic of other entities being able to check if they hit the player and call the player's receiveDamage method will work as normal.

Step 2: actually make the player invulnerable

First, let's set up the behaviour for the player becoming invulnerable. New function in the player's entity:

	becomeInvulnerable: function() {
		this.invulnTimer = new ig.Timer(this.invulnTime);
	},

So now we only have to call the becomeInvulnerable method and the timer that we check in the receive-damage routine will be checked, and note that we're setting it up for the time that it's running - it's made a variable so it's easier to check later.

Now, what we need to do in this entity when colliding, is see if we're colliding with the player and if so, call the right function to set up the timer.

In the entity for your star/invuln item, make sure that the player entity is declared in the list of requires up front, because we're going to be referencing it here.

When we do collisions, the check function of an entity will be called. In this case, we need to see if the entity we're colliding with is a player and if so, call the player's method:

	check: function (other) {
		if (other instanceof EntityPlayer) {
			other.becomeInvulnerable();
		}
	},

And that's it. The star entity knows how to check for and invoke player invulnerability, and the player entity knows how to become invulnerable and make use of that fact later on when taking 'damage'.

1 decade ago by sebastianvivaldi

Hi and thanks for help. The code doesn't work I think the timer does not start and the player does not become invulnerable. Any idea why that happens.

Hope you can help me. Thanks.

1 decade ago by Arantor

So, any errors in the JavaScript console?

1 decade ago by sebastianvivaldi

No errors in javascript console.
becomeInvulnerable function works because if I add something like this.health = 30; the player health change to 30. Any idea why the timer doesn't start?

1 decade ago by LaserBeam

I think you need the timer in the update function, so it can count down on every update.

1 decade ago by Arantor

As I understood it, the timer should be counting down regardless but I think you're right in that something needs to be in the update function so it can switch the state.

1 decade ago by LaserBeam

Yes, I did not make myself clear enough. The counter counts down anyway however you need in the update function the timer to check if 0, or whatever value you like, is reached. Then it workds.

1 decade ago by sebastianvivaldi

Thanks so much Arantor and LaserBeam. Here's the final working code I'm using (in case anyone needs it):

Player.

Properties:

noDamageTimer: new ig.Timer(),
isNoDamageMode: false,

Init:

this.noDamageTimer.set(10;

update:

if(this.noDamageTimer.delta()>0){
				
				this.isNoDamageMode = false;
}

Enemy.

check: function( other ) {
		if (other instanceof EntityPlayerNinja) {
			if(other.isNoDamageMode){
				this.kill();			
			}else{
				other.receiveDamage( 10, this);
				this.kill();
			}
       }
},


Star.

check: function (other) {
        	if (other instanceof EntityPlayerNinja) {
        		if(!other.isNoDamageMode){
        			other.isNoDamageMode = true;
        			other.noDamageTimer.reset();
        		}
        		
            }
            
            this.kill();
},

Thanks again

1 decade ago by sebastianvivaldi

Now I'm trying to change the animation when the player collides with the star. Know any way to do this?

I've searched in the forums and I've read some posts and I don't understand how to do.

1 decade ago by LaserBeam

##this.currentAnim = this.anims.invulnerable;##

1 decade ago by LaserBeam

this.currentAnim = this.anims.invulnerable;

1 decade ago by sebastianvivaldi

I know the code to set an animation.
My cuestion is how to start an animation, touch something and put another animation and then return to the original?
I can spawn an animation but what are the next steps?

1 decade ago by LaserBeam

Set the animation for invulnerability in the code when you touch the star, and in the code where the time runs out you go back to the original animation.
Page 1 of 1
« first « previous next › last »