Being Hurt and Reacting
The actual script for hurting your own baddy isn't really a function, you just have to know where to put it.
However, usually the way in which a baddy reacts to being hurt can be described as a function.
Its purpose is to move the NPC away from whatever caused it harm, so that it can be safe for a second or two.
 
function BounceBack(){ 
  Hunt()
  this.count=0; 
  while(this.count<5){ 
    sprite=39; 
    this.count+=.5; 
    if (x>this.testx&&!onwall(x+3.5,y+1.5)) x+=.5; 
    if (x<this.testx&&!onwall(x-.5,y+1.5)) x-=.5; 
    if (y>this.testy&&!onwall(x+1.5,y+3.5)) y+=.5; 
    if (y<this.testy&&!onwall(x+1.5,y+.5)) y-=.5; 
    sleep .05; 
  } 
  sprite=0; 
  timeout=.05; 
}
This function begins by calling the Hunt() function; after doing so we know the baddy's target x and y cords, denoted by this.testx and this.testy .  I initialize this.count as 0 because it is going to count up to keep track of how much the baddy has "bounced" back.  That is when the while loop starts which will run so long as this.count is less than 5.  From there I set the sprite equal to 39 (which is the hurt sprite), and I add .5 to this.count.  Then I run a rather large onwall detection script to see if the baddy can be moved away from its target, and if so, the movement will occur right away.  Next, the NPC will sleep for .05 seconds (I used sleep because while the NPC is being hurt you do not want it to receive any other commands), and then the loop starts over again moving the baddy just a little bit each time.  Once the loop has executed 10 times (because this.count will now be equal to 5), then the sprite is reset to 0 so the NPC can walk again, and the timeout var is initialized once more.
This function needs to be called within a if statement that is checking for the method by which you want your baddy to receive damage.
 
Here is an example of how my baddy's hurt detection stuff looks:
if ((washit||waspelt||exploded||wasshot)&& hearts>0){ 
  this.mode=0; 
  hearts-=playerswordpower; 
  BounceBack()
}
The first part of this is what checks to see if the baddy has been hurt.  This is all the usual stuff like the baddy was hit by a sword/weapon, pelt, exploded by a bomb, or shot.  But of course at least one of those has to be true AND the baddy still has to have life left ( hearts>0).  Then this.mode is set to 0 to stop any attacking functions that the baddy may be executing.  Next, the baddy loses the amount of hearts equal to the player's swordpower.  Finally, the aformentioned function BounceBack() is called to finish off the hurt animation.
This statement stands alone.  It does not need to be within a timeout loop, or anything like that.
 
Previous Topic         Home           Next Topic