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'.